hdu3586 树形dp+二分求解

25 篇文章 0 订阅
18 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=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

/**
hdu 3586  树形dp二分求解
题目大意:给定一棵树n个点,点1是根节点,每个叶子节点都向根节点传递“消息”,我们要在一些路上阻止,使路径不通。每条路上的阻止代价为该路权值,
          我们能阻止的权值有一个上限x,而且所有叶子节点阻止的代价和不能超过m。问最小的可行x
解题思路:假设我们已经到了一个x,那么以u为根节点的子树的总花费dp[u],若边权值w<=x,dp[u]+=min(dp[v],w),否则dp[u]+=min(dp[v],inf),
           这里的inf为任意一个大于m的数(但注意不要爆掉int,我取的m+1)。解释一下为什么用inf,因为w值已经超过限制我们不能阻止w边了,
           那么要阻止以v为根节点子树的所有叶子节点,只能是dp[v]了。这样树形dp就可以求出dp[1],和m比较即可知道我们的x值合不合适了。
           对于x二分就可以了。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1005;

struct note
{
    int v,w,next;
}edge[maxn*2];

int head[maxn],ip;
int n,m,mid,dp[maxn];

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

void addedge(int u,int v,int w)
{
   edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}

void dfs(int u,int pre)
{
    int flag=0;
    for(int i=head[u];i!=-1;i=edge[i].next)///求解父亲节点前它的所有儿子节点必须全部已经求出
    {
        int v=edge[i].v;
        if(v==pre)continue;
        flag=1;
        dfs(v,u);
    }
    if(flag==0)///叶子节点
    {
        dp[u]=m+1;///任意一个大于m的数
        return;
    }
    int t=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int w=edge[i].w;
        int v=edge[i].v;
        if(v==pre)continue;
        if(w<=mid)
           dp[u]+=min(dp[v],w);
        else
           dp[u]+=min(dp[v],m+1);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        int maxx=0;
        init();
        for(int i=0;i<n-1;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            maxx+=w;
        }
        int l=1,r=maxx;
        int flag=0;
        while(l<r)
        {
            mid=(l+r)>>1;
            memset(dp,0,sizeof(dp));
            dfs(1,-1);
          //  printf("%d %d\n",mid,dp[1]);
            if(dp[1]<=m)
            {
                flag=1;
                r=mid;
            }
            else
                l=mid+1;
        }
        if(flag==0)
            printf("-1\n");
        else
            printf("%d\n",r);
    }
    return 0;
}
/**
5 4
1 3 2
1 4 3
3 5 5
4 2 6

5 4
1 3 2
1 4 2
3 5 5
4 2 6

5 3
1 3 2
1 4 2
3 5 5
4 2 6


*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值