hdu 3586 Information Disturbing (树形dp+二分)

来源:互联网 发布:linux运维项目经验 编辑:程序博客网 时间:2024/05/25 23:57

Information Disturbing

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


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 51 3 21 4 33 5 54 2 60 0
 

Sample Output
3
 

删除边使树根和每个叶子节点断开,断开的边权值尽量小,且总权值和不大于m;
二分边权值,在树上进行动态规划,求断开后权值和最小值。
对于每个节点u,用dp[u]记录断开它所有子孙节点的最小权值和,
枚举它的每一个子节点,对于当前子节点,断开它的代价为dp[u]=min(dp[v],u-v边的权值);

#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<vector>using namespace std;#define ll long long#define N 1100#define mem(a,t) memset(a,t,sizeof(a))const int inf=1000005;struct node{    int v,w,next;}e[N*2];int dp[N];int head[N];int vis[N];int cnt,n;void add(int u,int v,int w){    e[cnt].v=v;    e[cnt].w=w;    e[cnt].next=head[u];    head[u]=cnt++;}void tree_dp(int u,int limit){    int i,v,flag=0,tmp=0;       //flag标记是否是叶子节点    for(i=head[u];i!=-1;i=e[i].next)    {        v=e[i].v;        if(!vis[v])        {            vis[v]=1;            flag=1;            tree_dp(v,limit);            if(e[i].w>limit)         //当前边权值大于限制,只能取dp[v]                tmp+=dp[v];            else                     //取最优解                tmp+=min(dp[v],e[i].w);        }    }    if(flag)                  dp[u]=tmp;    else            //叶子节点权值为inf        dp[u]=inf;}int main(){    //freopen("in.txt","r",stdin);    int i,u,v,m,w;    int l,r,mid,ans;    while(scanf("%d%d",&n,&m),n||m)    {        mem(head,-1);        cnt=0;        l=r=1;        for(i=1;i<n;i++)        {            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);            add(v,u,w);            r=max(r,w);        }        ans=-1;        while(l<=r)        {            mid=(l+r)>>1;            for(i=1;i<=n;i++)                vis[i]=dp[i]=0;            vis[1]=1;            tree_dp(1,mid);                    if(dp[1]<=m)            {                r=mid-1;                ans=mid;            }            else                l=mid+1;        }        printf("%d\n",ans);    }    return 0;}




0 0
原创粉丝点击