不要过于沉溺过去,也不要过于畅想未来,把握现在!

HDU 3586 树状dp

Information Disturbing

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


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
 

 

Author
alpc86
 

 

Source

HDU 3586:

题目意思;

打断一棵树的叶子节点和根节点之间的联系,要求只有士兵的能力大于这条边所需要的能力时才能被打断,同时满足所有消耗的能力值之和不大于m,求士兵所具有的最小的能力值;


解题思路:

首先对士兵的能力值在一个区间内进行二分,然后对于每一个值进行dp,状态转移方程为:

if(dp[son]>cost[father][son]&&cost[father][cost]<=mid)

dp[son]=cost[father][son];

然后求到根节点的时候,把根节点的儿子节点的所有dp值都加起来如果小于等于m,则返回真值,否则,返回0

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 using namespace std;
 8 const  int maxn=2010;
 9 const int inf=1000001;
10 vector<int> G[maxn];
11 int cost[maxn][maxn];
12 int n,m,head,mid;
13 int dfs(int parent,int point)
14 {
15     int ans=0;
16     for(int i=0;i<G[point].size();i++)
17     {
18          if(G[point][i]==parent) continue;
19          int result=dfs(point,G[point][i]);
20          if(cost[point][G[point][i]]<=result&&cost[point][G[point][i]]<=mid)
21          {
22              result=cost[point][G[point][i]];
23          }
24          ans+=result;
25     }
26     if(!ans) return inf;
27     return ans;
28 }
29 int main()
30 {
31  //  freopen("in.txt","r",stdin);
32     int u,v,c;
33     while(~scanf("%d%d",&n,&m)){
34         if(!n&&!m) break;
35         int l=inf,r=0;
36         for(int i=0;i<maxn;i++) while(!G[i].empty()) G[i].pop_back();
37         for(int i=0;i<n-1;i++)
38         {
39             scanf("%d%d%d",&u,&v,&c);
40             if(c<l) l=c;
41             if(c>r) r=c;
42             G[u].push_back(v);
43             G[v].push_back(u);
44         cost[u][v]=cost[v][u]=c;
45         }
46         int ans=-1;
47         while(l<=r){
48             mid=(l+r)>>1;
49             if(dfs(0,1)<=m){
50                 ans=mid;
51                 r=mid-1;
52             }
53             else l=mid+1;
54         }
55          printf("%d\n",ans);
56     }
57     return 0;
58 }

 

posted @ 2015-03-03 23:26  coding_yuan  阅读(647)  评论(0编辑  收藏  举报

不要过于沉溺过去,也不要过于畅想未来,把握现在!