首页 > 代码库 > HDU 3586 - Information Disturbing

HDU 3586 - Information Disturbing

Information Disturbing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2693    Accepted Submission(s): 960


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 communication routes can cover all of their soldiers. Information can exchange between any two soldiers by the communication routes. The number 1 soldier is the total commander and other soldiers who have only one neighbour is the frontline soldier.
Your boss zzn ordered you to cut off some routes to make any frontline soldiers in the network cannot reflect the information they collect from the battlefield to the total commander( number 1 soldier).
There is a kind of device who can choose some routes to cut off . But the cost (w) of any route you choose to cut off can’t be more than the device’s upper limit power. And the sum of the cost can’t be more than the device’s life m.
Now please minimize the upper limit power of your device to finish your task.
 
Input
 
  The input consists of several test cases.
The first line of each test case contains 2 integers: n(n<=1000)m(m<=1000000).
Each of the following N-1 lines is of the form:
ai bi wi
It means there’s one route from ai to bi(undirected) and it takes wi cost to cut off the route with the device.
(1<=ai,bi<=n,1<=wi<=1000)
The input ends with n=m=0.

 

Output
 
  Each case should output one integer, the minimal possible upper limit power of your device to finish your task.
If there is no way to finish the task, output -1.
 

 

Sample Input
 
5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0
 
Sample Output
 
3
 
这道题目绝对的坑
题意:
  一个树上,1为司令部,叶子节点为敌军,要切断司令部与敌军的所有道路,且总花费不超过m,求切断边中最大的权值最小为多少
思路:
  二分法,
AC代码:
技术分享
 1 # include <bits/stdc++.h> 2 using namespace std; 3 const int MAXN = 1010; 4 const int INF = 3000010; // 注意,这里是个坑,容易爆了 5 struct Node 6 { 7     int to; 8     int next; 9     int w;10 }edge[MAXN*2];11 int head[MAXN];12 int tol;13 int dp[MAXN];14 void add(int a,int b,int w)15 {16     edge[tol].to = a;17     edge[tol].next = head[b];18     edge[tol].w = w;19     head[b] = tol++;20 }21 22 void dfs(int root, int f, int limit)23 {24     int flag = false;//标记是不是叶子结点25     dp[root] = 0;26     for(int i = head[root]; i != -1; i = edge[i].next)27     {28         int son = edge[i].to;29         if(son == f)30             continue;31         flag = true;32         dfs(son, root, limit);33         if(edge[i].w <= limit)34             dp[root] += min(dp[son], edge[i].w);35         else36             dp[root] += dp[son];37     }38     if(!flag)39         dp[root] = INF;//叶子结点无穷大40 }41 int main()42 {43     int n, m;44     while(scanf("%d%d",&n, &m) != EOF)45     {46         if(n == 0 && m == 0)47             break;48         tol = 0;49         memset(head, -1, sizeof(head));50         int Max = 0, a, b, w;51         for(int i = 1; i < n; i++)52         {53             scanf("%d%d%d", &a, &b, &w);54             add(a, b, w);55             add(b, a, w);56             if(w > Max)57                 Max = w;58         }59         int Min = 1, mid;60         int ans = -1;61         while(Min <= Max)62         {63             mid = (Min + Max) / 2;64             dfs(1, -1, mid);65             if(dp[1] <= m)66             {67                 ans = mid;68                 Max = mid - 1;69             }70             else71                 Min = mid + 1;72         }73         printf("%d\n", ans);74     }75     return 0;76 }
View Code

 

HDU 3586 - Information Disturbing