地址:http://acm.hdu.edu.cn/showproblem.php?pid=3586
Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
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.
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.
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.
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
思路:树形DP+二分,利用树形DP求在不超过限制的情况下,切断联系的总消耗的最小值,然后根据最小值是否大于m来改变左右端点。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define LL __int64
#define Ma 1010
struct node{
int to,next,vi;
}tree[Ma*2];
int n,ma,len,head[Ma];
void add(int pa,int to,int vi){
tree[len].to=to;
tree[len].vi=vi;
tree[len].next=head[pa];
head[pa]=len++;
}
int dfs(int no,int pa,int k){
int to,ans1=ma+1,ans2=0; //ans1表示子节点与父亲节点的切断消耗,ans2表示子节点与其子节点的切断消耗
for(int i=head[no];i!=-1;i=tree[i].next){
to=tree[i].to;
if(to==pa){
if(tree[i].vi<=k) ans1=tree[i].vi;
continue;
}
ans2+=dfs(to,no,k);
}
if(!ans2) ans2=ma+1; //若连线切断消耗均大于限制,就将其总消耗赋值为ma+1
return min(ans1,ans2);
}
int main(){
while(scanf("%d%d",&n,&ma)>0,n|ma){
memset(head,-1,sizeof(head));
len=0;
int l,r,v;
for(int i=1;i<n;i++){
scanf("%d%d%d",&l,&r,&v);
add(l,r,v);
add(r,l,v);
}
l=1,r=ma;
while(l<=r){ //二分
int mid=(l+r)/2;
int k=dfs(1,-1,mid);
if(k<=ma) r=mid-1; //根据切断联系的总消耗的最小值来改变左右端点
else l=mid+1;
}
printf("%d\n",l>ma?-1:l); //注意-1对应的情况,我因为没有处理-1WA了一次
}
return 0;
}