zoukankan      html  css  js  c++  java
  • poj 1062 昂贵的聘礼(dfs+剪枝)

    http://poj.org/problem?id=1062

      网上都说是dij,可是我想不到怎么dij。。。- -所以就只好暴力dfs+剪枝来做这题了。实践过,也就47ms,不会太长时间。这题注意的是,最高等级和最低等级的差不超过给定的值的判断。暴力搜索加上剪枝,剪枝的方法只需(1)跟目前找到的最小价格比较,判断是否继续找下去(2)判断是否曾经搜索了。

    通过代码:

    View Code
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <iomanip>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <string>
     9 #include <queue>
    10 #include <ctime>
    11 
    12 using namespace std;
    13 
    14 #define PB push_back
    15 #define FI first
    16 #define SE second
    17 #define MPR make_pair
    18 #define REP(i, n) for (int i = 0; i < n; i++)
    19 #define REP_1(i, n) for (int i = 1; i <= n; i++)
    20 #define FORI(i, a, b) for (int i = a; i < b; i++)
    21 #define FORD(i, a, b) for (int i = a; i > b; i--)
    22 #define _clr(x) memset(x, 0, sizeof(x))
    23 #define _rst(x) memset(x, -1, sizeof(x))
    24 
    25 typedef long long LL;
    26 typedef pair<int, int> PII;
    27 typedef vector<LL> VLL;
    28 typedef vector<PII> VPII;
    29 typedef vector<int> VI;
    30 typedef vector<double> VDBL;
    31 const int N = 105;
    32 const int hashMod = 1e6 + 5;
    33 const int inf = 0x55555555;
    34 const double eps = 1e-8;
    35 const LL linf = 0x5555555555555555ll;
    36 const double finf = 1e50;
    37 const double pi = acos(-1.0);
    38 
    39 LL minCost;
    40 int level;
    41 bool vis[N];
    42 struct Good {
    43     int price, level;
    44     VPII sub;
    45     Good(int _p = 0, int _l = 0) {
    46         price = _p, level = _l;
    47         sub.clear();
    48     }
    49 } good[N];
    50 
    51 void input(int n) {
    52     int p, l, m;
    53     REP_1(i, n) {
    54         cin >> p >> l >> m;
    55         good[i] = Good(p, l);
    56         REP(j, m) {
    57             cin >> p >> l;
    58             good[i].sub.PB(MPR(p, l));
    59         }
    60     }
    61     minCost = finf;
    62     _clr(vis);
    63 }
    64 
    65 void dfs(LL curCost, int cur, int mn, int mx) {
    66     int sz = good[cur].sub.size();
    67     vis[cur] = true;
    68     minCost = min(curCost + good[cur].price, minCost);
    69     REP(i, sz) {
    70         int id = good[cur].sub[i].FI, cost = good[cur].sub[i].SE;
    71         if (!vis[id] && max(mx, good[id].level) - min(mn, good[id].level) <= level) {
    72             if (curCost + cost <= minCost) {
    73                 dfs(curCost + cost, id, min(mn, good[id].level), max(mx, good[id].level));
    74             }
    75         }
    76     }
    77     vis[cur] = false;
    78 }
    79 
    80 int main() {
    81     int n;
    82     while (cin >> level >> n) {
    83         input(n);
    84         dfs(0, 1, good[1].level, good[1].level);
    85         cout << minCost << endl;
    86     }
    87     return 0;
    88 }

    ——written by Lyon

  • 相关阅读:
    TCP协议详解-IPv4
    welcome to my cnblog
    怎样解决闭包造成的内存泄漏
    跳转路由后请求失败
    vant grid组件图片加载问题
    3次握手
    res.send()传参----Invalid status code: 1
    堆栈总结
    jQuery实现全选
    phpstudy_pro打开MySQL服务,一闪一闪的
  • 原文地址:https://www.cnblogs.com/LyonLys/p/poj_1062_Lyon.html
Copyright © 2011-2022 走看看