标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2693 Accepted Submission(s): 960
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 else 36 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 else 71 Min = mid + 1; 72 } 73 printf("%d\n", ans); 74 } 75 return 0; 76 }
HDU 3586 - Information Disturbing
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5815397.html