P - Information Disturbing (树形dp+二分)

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

题意:

一棵树,每条边,都有成本,使得1和其他点没有联系,需要切断边,在不超过总成本的前提下,最低的成本上线是多少。
思路:(求最小值的最大值,一看这种形式首先想到二分,关键部分是要找到二分来找最小的下线成本)
可以枚举所有的成本,找出最小的下线成本,用二分枚举,用dfs遍历一边树,看是否符合不超过总成本。

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=1100;
#define inf 1000005
int n,m;
int dp[maxn];
int head[maxn];
int vis[maxn];
int tot,maxx;

struct Edge{
int next;
int to;
int len;
}edge[maxn*3];

void add(int u,int v,int len){
edge[++tot].next=head[u];
edge[tot].to=v;
edge[tot].len=len;
head[u]=tot;
}

void dfs(int x,int pa,int mid)
{
    dp[x]=0;
    int p=0;
    for(int i=head[x];i!=0;i=edge[i].next)
    {
        int next=edge[i].to;
        if(next!=pa)
        {
            p=1;
            int w=edge[i].len;
            dfs(next,x,mid);
            if(w>mid)dp[x]+=dp[next];
            else dp[x]+=min(w,dp[next]);

        }
    }
    if(!p)dp[x]=inf;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        if(n==0&&m==0)break;
        tot=0;
        maxx=0;
        memset(head,0,sizeof(dp));
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n-1;i++){
            int u,v,len;
            cin>>u>>v>>len;
            add(u,v,len);
            add(v,u,len);
            maxx=max(maxx,len);
        }
        int l=1,r=maxx,mid;
        int ans=inf;
        while(l<=r){
            mid=(r+l)>>1;
            dfs(1,-1,mid);
            if(dp[1]<=m){
                ans=mid;
                r=mid-1;
            }
            else
                l=mid+1;
        }
        if(ans==inf)cout<<"-1"<<endl;
        else
            cout<<ans<<endl;

    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面有篇英文课文,请编程找出课文中所有的单词,统计单词出现的次数,统计时不区分字母的大小写,最后按出现的次数从大到小显示出现3次以上各个单词及次数。例如,结果显示如下: the 18 a 14 puma 9 in 8 it 8 ...(略) 英文课文如下: Pumas are large, cat-like animals which are found in America. When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously. However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar. The hunt for the puma began in a small village where a woman picking blackberries saw 'a large cat' only five yards away from her. It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered. The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening. Wherever it went, it left behind it a trail of dead deer and small animals like rabbits. Paw prints were seen in a number of places and puma fur was found clinging to bushes. Several people complained of 'cat-like noises' at night and a businessman on a fishing trip saw the puma up a tree. The experts were now fully convinced that the animal was a puma, but where had it come from ? As no pumas had been reported missing from any zoo in the country, this one must have been in the possession of a private collector and somehow managed to escape. The hunt went on for several weeks, but the puma was not caught. It is disturbing to think that a dangerous wild animal is still at large in the quiet countryside.
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值