Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2667 Accepted Submission(s): 949
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
Author
alpc86
Source
Recommend
zhouzeyong
思路:一看就是树形dp,单论存不存在这是一道水题,直接dp就好,明显有动态转移方程:dp[根]+=max(dp[子],边权值),但问题要求出最大边权值,这里要巧妙的利用二分求出答案,把边权值在所要范围内的才累加,若最后dp[1]>m,证明范围太小,要扩大范围,反之缩小范围,然后一直重复dp直到求出答案,若范围给到最大还是>m,证明不存在。下面给代码:
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#define maxn 10005
#define inf 0x3f3f3f3f
using namespace std;
int head[maxn],dp[maxn];
int n,m;
struct node{
int u,value,next;
}p[maxn<<1];
void dfs(int x,int fa,int limit){
bool jud=true;
dp[x]=0;
for(int i=head[x];~i;i=p[i].next){
int next=p[i].u;
if(next==fa)
continue;
dfs(next,x,limit);
jud=false;
if(p[i].value<=limit){
dp[x]+=min(dp[next],p[i].value);
}
else{
dp[x]+=dp[next];
dp[x]=min(dp[x],inf);
}
}
if(jud)
dp[x]=inf;
}
int main(){
while(scanf("%d%d",&n,&m)&&(n+m)){
memset(head,-1,sizeof(head));
int maxnum=0;
for(int i=0;i<((n-1)<<1);i++){
int u,v,value;
scanf("%d%d%d",&u,&v,&value);
p[i].u=u;
p[i].value=value;
p[i].next=head[v];
head[v]=i++;
p[i].u=v;
p[i].value=value;
p[i].next=head[u];
head[u]=i;
maxnum=max(maxnum,value);
}
int l=1,r=maxnum,mid;
int ans=-1;
while(l<=r){
mid=(l+r)>>1;
dfs(1,0,mid);
if(dp[1]<=m){
r=mid-1;
ans=mid;
}
else{
l=mid+1;
}
}
printf("%d\n",ans);
}
}