HDU 3586 Information Disturbing (二分+树形dp)
2016-09-21 21:08
423 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586
给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=10^6
dp[i]表示i节点为root的这个子树所破坏的最少费用,if(cost[i][i->son] <= limit) dp[i] += min(dp[i->son], cost[i][i->son]);
二分limit,然后把limit放到dfs中判断是不是都能切断叶子节点的联系。
给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=10^6
dp[i]表示i节点为root的这个子树所破坏的最少费用,if(cost[i][i->son] <= limit) dp[i] += min(dp[i->son], cost[i][i->son]);
二分limit,然后把limit放到dfs中判断是不是都能切断叶子节点的联系。
#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; typedef long long LL; typedef pair <int, int> P; const int N = 1e5 + 5; int to[N << 1], Next[N << 1], cost[N << 1], head , tot, dp , inf = 1e6 + 7; void init() { memset(head, -1, sizeof(head)); tot = 0; } inline void add_edge(int u, int v, int c) { Next[tot] = head[u]; to[tot] = v; cost[tot] = c; head[u] = tot++; } void dfs(int u, int p, int limit) { dp[u] = inf; bool inter = false; for(int i = head[u]; ~i; i = Next[i]) { int v = to[i]; if(v == p) continue; dfs(v, u, limit); if(!inter) { dp[u] = 0; inter = true; } if(cost[i] <= limit) { dp[u] += min(dp[v], cost[i]); } else { dp[u] += dp[v]; } } } void solve() { int n, m, u, v, c; while(~scanf("%d %d", &n, &m) && (n || m)) { init(); for(int i = 1; i < n; ++i) { scanf("%d %d %d", &u, &v, &c); add_edge(u, v, c); add_edge(v, u, c); } int l = 1, r = 1001; while(l < r) { int mid = (l + r) >> 1; dfs(1, -1, mid); if(dp[1] <= m) { r = mid; } else { l = mid + 1; } } if(r == 1001) { printf("-1\n"); } else { printf("%d\n", l); } } } int main() { solve(); return 0; }
相关文章推荐
- HDU 3586 Information Disturbing(二分+树形dp)
- HDU - 3586 Information Disturbing【树形dp+二分】
- hdu 3586 Information Disturbing (树形dp+二分)
- HDU 3586 Information Disturbing (树形DP+二分)
- HDU 3586 Information Disturbing (树形DP+二分)
- HDU 3586 Information Disturbing 树形DP+二分
- HDU 3586 - Information Disturbing(二分+树形DP)
- HDU 3586 Information Disturbing (树形dp+二分)
- hdu 3586 Information Disturbing (树形dp+二分)
- hdu 3586 Information Disturbing 树形dp+二分
- hdu 3586 树形dp +二分
- HDU - 3586 Information Disturbing 【二分 + 树形dp】
- HDU-3586 Information Disturbing 树形dp+二分
- HDU 3586 Information Disturbing 二分+树形DP
- hdu 3586(树形dp+二分)
- hdu 3586 树形dp+二分
- hdu 3586 (树形dp+二分)
- hdu 3586 Information Disturbing(树形dp + 二分)
- 【HDU】3586 Information Disturbing 二分+树形dp
- HDU 3586【树形DP+二分】