HDU 3586 Information Disturbing (二分+树形dp)

题目链接: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中判断是不是都能切断叶子节点的联系。

 1 #pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 1e5 + 5;
17 int to[N << 1], Next[N << 1], cost[N << 1], head[N], tot, dp[N], inf = 1e6 + 7;
18
19 void init() {
20     memset(head, -1, sizeof(head));
21     tot = 0;
22 }
23 inline void add_edge(int u, int v, int c) {
24     Next[tot] = head[u];
25     to[tot] = v;
26     cost[tot] = c;
27     head[u] = tot++;
28 }
29 void dfs(int u, int p, int limit) {
30     dp[u] = inf;
31     bool inter = false;
32     for(int i = head[u]; ~i; i = Next[i]) {
33         int v = to[i];
34         if(v == p)
35             continue;
36         dfs(v, u, limit);
37         if(!inter) {
38             dp[u] = 0;
39             inter = true;
40         }
41         if(cost[i] <= limit) {
42             dp[u] += min(dp[v], cost[i]);
43         } else {
44             dp[u] += dp[v];
45         }
46     }
47 }
48 void solve() {
49     int n, m, u, v, c;
50     while(~scanf("%d %d", &n, &m) && (n || m)) {
51         init();
52         for(int i = 1; i < n; ++i) {
53             scanf("%d %d %d", &u, &v, &c);
54             add_edge(u, v, c);
55             add_edge(v, u, c);
56         }
57         int l = 1, r = 1001;
58         while(l < r) {
59             int mid = (l + r) >> 1;
60             dfs(1, -1, mid);
61             if(dp[1] <= m) {
62                 r = mid;
63             } else {
64                 l = mid + 1;
65             }
66         }
67         if(r == 1001) {
68             printf("-1\n");
69         } else {
70             printf("%d\n", l);
71         }
72     }
73 }
74
75 int main()
76 {
77     solve();
78     return 0;
79 }
时间: 2024-10-16 23:52:31

HDU 3586 Information Disturbing (二分+树形dp)的相关文章

hdu 3586 Information Disturbing (树形dp)

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1722    Accepted Submission(s): 641 Problem Description In the battlefield , an effective way to defeat enemies is to bre

HDU 3586 Information Disturbing (树形DP,二分)

题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一定的power,而我们有一台具有m点power的机器,问在使用此机器切断敌人通信系统的情况下,使得所切断边的最大边权达到最小是多少?(m<=100w,n<=1000) 思路: 其实就是要求所切断的边的最小瓶颈边.由于m比较大,不好枚举或者DP.但是他们具有线性的关系,就是瓶颈边越大,肯定越容易有解

HDU 3586 Information Disturbing

二分+树形DP验证. 答案是通过二分查找得到的,对于每一次二分到的值,进行验证,是否可行. 可以用树形DP来求解所有叶子节点不能向根节点传送消息的最小费用,dp[i] 表示 节点i 的子树的叶子结点都不能向i传送消息的最小费用,这样很容易递推. #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<map> #include<vector>

HDU 3586 Information Disturbing(二分+树形dp)

http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit. 思路: 对于上限limit我们可以二分查找.然后就是树形dp,看代码就可以理解的. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring>

HDU 3586 Information Disturbing 树形DP+二分

Information Disturbing Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system.The information department told you that there are n enemy soldiers and their network which have n-1 communicati

HDU 3586.Information Disturbing 树形dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3205    Accepted Submission(s): 1137 Problem Description In the battlefield , an effective way to defeat enemies is to bre

HDU-3586 Information Disturbing(树形DP+删边)

题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的limit的可能取值. 题目分析:二分枚举limit,定义状态dp(u)表示将u与它管辖的叶子节点失去联系所需要的总代价,则: dp(u)+=min(dp(son),e[i].w),e[i].w<=limit: dp(u)+=dp(son)  e[i].w>limit: 代码如下: # inclu

hdu 4118 Holiday&#39;s Accommodation 树形dp

Holiday's Accommodation Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4118 Description Nowadays, people have many ways to save money on accommodation when they are on vacation.One of these ways is exchanging

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m