HDU 3586 树形dp

Information Disturbing

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


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
 题意:
n个点,n-1条带权无向边,1为根节点,目标是切断所有叶子节点与1点之间的连接,限制:总的花费不超过m,要切割的每条边的权值不能大于w,求出达到目的的最小的w
代码:
//从下到上处理当到了x时的最小花费是切断x与儿子节点y之间的边和切断y子树中的
//某几条边中小的值,如此更新节点值最后1点的花费就是答案;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=1000009;
const int maxn=1009;
int n,m,head[maxn],tol,val[maxn],vis[maxn];
struct Edge{
    int to,w,next;
}edge[maxn*2];
void Add(int x,int y,int z){
    edge[tol].to=y;
    edge[tol].w=z;
    edge[tol].next=head[x];
    head[x]=tol++;
}
void dfs(int x,int fa,int mid){
    val[x]=0;
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].to;
        if(y==fa) continue;
        dfs(y,x,mid);
        if(edge[i].w<=mid)
            val[x]+=min(val[y],edge[i].w);
        else val[x]+=val[y];
    }
    if(val[x]==0) val[x]=inf;
}
int main()
{
    while(scanf("%d%d",&n,&m)&&(n+m)){
        memset(head,-1,sizeof(head));
        tol=0;
        int l=inf,r=0,mid;
        for(int i=1;i<n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            Add(x,y,z);Add(y,x,z);
            l=min(l,z);
            r=max(r,z);
        }
        int ans=-1;
        while(l<=r){
            mid=(l+r)>>1;
            dfs(1,0,mid);
            if(val[1]<=m){
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2017-05-18 15:18  luckilzy  阅读(276)  评论(0编辑  收藏  举报