hdu3586(树形dp)
Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2351 Accepted Submission(s): 845
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.
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.
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.
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,问删掉边中最大权的最小值能是多少。
思路:
dp[i][j]表示在i结点为根的子树中,使其叶子到达不了根的,删掉边的最大权小于等于j的最小被删边权和。
因为这题生平第一次因为INF的设置不能太大而wa无数次!!!!主要是因为dp初始化全部设置的INF,那么tem数组累加的时候就会爆int。。。。。真的是心累。。。跟别人的博客代码都改得几乎一样了结果我的就是错的。。。。无语……
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> #include<queue> using namespace std; typedef long long ll; const int INF=1100000; struct data { int to,w; }; vector<data>tu[1111]; int m,n; int dp[1111][1010]; int ml; void dfs(int now,int w) { if(now!=1) for(int i=w; i<=ml; i++) dp[now][i]=w; int l=tu[now].size(); int tem[1010]= {0}; for(int i=0; i<l; i++) { int to=tu[now][i].to,w=tu[now][i].w; dfs(to,w); for(int j=1; j<=ml; j++) tem[j]+=dp[to][j]; } for(int i=1; i<=ml; i++) if(tem[i]) dp[now][i]=min(dp[now][i],tem[i]); } int main() { while(~scanf("%d%d",&n,&m)&&m+n) { for(int i=0; i<=n; i++) tu[i].clear(); ml=0; for(int i=0; i<n-1; i++) { int a,b,w; data hh; scanf("%d%d%d",&a,&b,&w); ml=max(ml,w); hh.to=b,hh.w=w; tu[a].push_back(hh); } for(int i=1; i<=n; i++) for(int j=1; j<=ml; j++) dp[i][j]=INF; dfs(1,0); int ans=-1; for(int i=1; i<=ml; i++) if(dp[1][i]<=m) { ans=i; break; } printf("%d\n",ans); } return 0; }
持续更新博客地址:
blog.csdn.net/martinue