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.
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.
If there is no way to finish the task, output -1.
5 5 1 3 2 1 4 3 3 5 5 4 2 6 0 0
3
题意:
一棵树,每条边,都有成本,使得1和其他点没有联系,需要切断边,在不超过总成本的前提下,最低的成本上线是多少。
思路:(求最小值的最大值,一看这种形式首先想到二分,关键部分是要找到二分来找最小的下线成本)
可以枚举所有的成本,找出最小的下线成本,用二分枚举,用dfs遍历一边树,看是否符合不超过总成本。
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=1100;
#define inf 1000005
int n,m;
int dp[maxn];
int head[maxn];
int vis[maxn];
int tot,maxx;
struct Edge{
int next;
int to;
int len;
}edge[maxn*3];
void add(int u,int v,int len){
edge[++tot].next=head[u];
edge[tot].to=v;
edge[tot].len=len;
head[u]=tot;
}
void dfs(int x,int pa,int mid)
{
dp[x]=0;
int p=0;
for(int i=head[x];i!=0;i=edge[i].next)
{
int next=edge[i].to;
if(next!=pa)
{
p=1;
int w=edge[i].len;
dfs(next,x,mid);
if(w>mid)dp[x]+=dp[next];
else dp[x]+=min(w,dp[next]);
}
}
if(!p)dp[x]=inf;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
if(n==0&&m==0)break;
tot=0;
maxx=0;
memset(head,0,sizeof(dp));
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n-1;i++){
int u,v,len;
cin>>u>>v>>len;
add(u,v,len);
add(v,u,len);
maxx=max(maxx,len);
}
int l=1,r=maxx,mid;
int ans=inf;
while(l<=r){
mid=(r+l)>>1;
dfs(1,-1,mid);
if(dp[1]<=m){
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
if(ans==inf)cout<<"-1"<<endl;
else
cout<<ans<<endl;
}
return 0;
}