hdu 3586 Information Disturbing 树形dp+二分

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

 

题目大意:有n个士兵,编号从1到n,它们组成一棵树,且编号1是指挥官,叶子节点的士兵是前线士兵,现在要你切断所有前线士兵与指挥官的通信,也就是删掉一些边。

其中每条边都有一个代价,删除一条边就要耗费相应代价。现在要你求总代价不超过m,使单条边最大代价最小,求这个最小值。

思路:因为有两个限定条件,这里就需要展现二分的魅力了,先进行二分确定单条边最大代价up,然后进行一次dfs求得单条边不超过up的最小总代价,根据最小总代价是否大于m更新下一次up的值。

当题目有两个限定条件不好考虑时,不妨使用二分先确定其中一个条件,然后只剩下一个限定条件就简单了。

 

让我又一次感受到了二分的无限魅力。

 

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int MAXN = 1005;
int head[MAXN],cur;
struct Edge{
    int cost;
    int next,to;
}edge[MAXN*2];
void addedge(int u,int v,int cost) {
    edge[cur].next = head[u];
    edge[cur].to=v;
    edge[cur].cost=cost;
    head[u] = cur++;
}
void init() {
    memset(head,-1,sizeof(head));
    cur=0;
}
int dp[MAXN];
int dfs(int u,int pre,int up) {
    //printf("%d %d %d\n",u,pre,up);
    int sum=0;
    bool isleaf=true;
    for(int e=head[u];e!=-1;e=edge[e].next) {
        int v=edge[e].to;
        if(v==pre) continue;
        isleaf=false;
        if(edge[e].cost<=up) sum+=min(edge[e].cost,dfs(v,u,up));
        else sum+=dfs(v,u,up);
    }
    if(isleaf) return 1000000+1;
    return sum;
}
int main()
{
    int n,m,u,v,w;
    while(scanf("%d%d",&n,&m)!=EOF&&n) {
        init();
        for(int i=1;i<n;i++) {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        int s=0,e=1000,ans=-1;
        int tmp;
        while(s<=e) {
            int mid = (s+e)/2;
            tmp = dfs(1,-1,mid);
            //printf("%d\n",tmp);
            if(tmp<=m) {
                ans=mid;
                e=mid-1;
            }
            else s=mid+1;
        }
        printf("%d\n",ans);
    }
}
posted @ 2016-04-09 20:23  ZhMZ  阅读(210)  评论(0编辑  收藏  举报