国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > hdu 3586 Information Disturbing (树形dp+二分)

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

来源:程序员人生   发布时间:2015-06-12 08:54:55 阅读次数:2965次

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⑴ 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⑴ 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 ⑴.
 

Sample Input
5 5 1 3 2 1 4 3 3 5 5 4 2 6 0 0
 

Sample Output
3
 

删除边使树根和每一个叶子节点断开,断开的边权值尽可能小,且总权值和不大于m;
2分边权值,在树上进行动态计划,求断开后权值和最小值。
对每一个节点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!=⑴;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,⑴); 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=⑴; 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⑴; ans=mid; } else l=mid+1; } printf("%d ",ans); } return 0; }




生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生