HDU 3586 Information Disturbing(二分答案+树形DP)

Information Disturbing

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

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
2010 ACM-ICPC Multi-University Training Contest(15)——Host by NUDT  

 

Recommend zhouzeyong        

题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=100万

 

解题思路:一看到题目就觉得是树形DP,男人的第六感怎一个准字了得。然后努力地把题目看懂了以后,发现这是一个判定性问题,就是问某个limit是否能够满足条件切断所有前线与司令部的联系,然后找符合条件的最大limit就可以了。想通这点以后就可以二分答案,下限为1,上限为最大的边权,再写一个Tree_DP(limit),判断切断所有前线联系时花费的最小费用是否小等于m,是的话就往后查找,否则向前。

    设dp[i]为切断i的所有子孙叶子所花费的最小费用,状态转移方程: if (i->son.len < limit) dp[i] += min(dp[i->son],i->son.len); (如果与子节点相连的边可选)

                                                                                                          else dp[i]  += dp[i->son];(如果与子节点相连的边不可选)

 

/*
HDU 3586
树形DP+二分答案
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1010;
const int INF=1000010;//这里一定要设得合适,不能太大,不能太小

struct Node
{
    int to;
    int next;
    int w;
}edge[MAXN*2];
int head[MAXN];
int tol;
int dp[MAXN];
void add(int a,int b,int w)
{
    edge[tol].to=a;
    edge[tol].next=head[b];
    edge[tol].w=w;
    head[b]=tol++;
    edge[tol].to=b;
    edge[tol].next=head[a];
    edge[tol].w=w;
    head[a]=tol++;
}

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

void dfs(int u,int pre,int limit)
{
    int flag=false;//标记是不是叶子结点
    dp[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==pre)continue;
        flag=true;
        dfs(v,u,limit);
        if(edge[i].w<=limit)dp[u]+=min(dp[v],edge[i].w);
        else dp[u]+=dp[v];
    }
    if(!flag)dp[u]=INF;//叶子结点无穷大
}
int main()
{
    int n,m;
    int a,b,w;
    int l,r,mid;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)break;
        init();
        r=0;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            add(a,b,w);
            if(w>r)r=w;
        }
        l=1;
        int ans=-1;
        while(l<=r)
        {
            mid=(l+r)/2;
            dfs(1,-1,mid);
            if(dp[1]<=m)
            {
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/29/2662751.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞