Information Disturbing hdu-3586

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
题解:在满足总的花费不大于m的情况下,limit要尽量小,所以二分limit,然后检查啦!(学过的东西不能融汇贯通,难怪我这么菜,orz)
#include<bits/stdc++.h>
using namespace std;

const int maxn=1005;struct node{
    int to,next,va;
}e[2*maxn];

int n,m,tot;
int dp[maxn],head[maxn];

void Inite(){
    tot=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w){
    e[tot].to=v;
    e[tot].va=w;
    e[tot].next=head[u];
    head[u]=tot++;
}

void DFS(int pa,int u,int limit){
    dp[u]=0;
    int flag=0;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].to;
        if(pa==v) continue;
        flag=1;
        DFS(u,v,limit);
        
        if(e[i].va>limit) dp[u]+=dp[v];
        else dp[u]+=min(dp[v],e[i].va);
    }
    if(flag==0) dp[u]=100001;
}

bool check(int limit){
    DFS(0,1,limit);
    if(dp[1]>m) return false;        
    else return true;
} 

void Solve(){
    int l=0,r=m+1;
    for(int i=1;i<=100;i++){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid;
    }
    if(check(l)) printf("%d\n",l);
    else if(check(r)) printf("%d\n",r);
    else printf("-1\n");
}

int main()
{    
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) break;
        
        Inite();
        for(int i=2;i<=n;i++){
            int u,v,va;
            scanf("%d%d%d",&u,&v,&va);
            addedge(u,v,va);
            addedge(v,u,va);
        }
        Solve();    
    }
    return 0;
}

 

posted @ 2017-11-01 23:40  天之道,利而不害  阅读(292)  评论(0编辑  收藏  举报