Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3444 Accepted Submission(s): 1212
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
题意:敌人要进攻司令部,然后我们要切断敌人与司令部之间的路,切断一条路需要代价,求使得敌人无法进攻司令部切断路中代价最大的一条路代价最小,并且在当前状况下,需要切边总和小于m。也就是删除边的和要小于m,其中代价最大的一条路代价最小。
求最大的最小,当时在做二分的题的时候就经常做,这道题当然要用二分了,在输入的时候找出边的最大值作为上限,然后dp就行了。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include<vector>
using namespace std;
struct f
{
int now,next,val;
}tree[2005];
int n,m,head[1005],p,maxx,flag,dp[1005];
void csh()
{
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
p=0;
maxx=-99999999;
}
void build(int fa,int son,int val)
{
tree[p].val=val;
tree[p].now=son;
tree[p].next=head[fa];
head[fa]=p++;
}
void dfs(int now,int pre)
{
dp[now]=0;
int limit=1;
for(int i=head[now];i!=-1;i=tree[i].next)
{
int pp=tree[i].now;
if(pp==pre)
continue;
limit=0;
dfs(pp,now);
if(tree[i].val<=flag)
dp[now]+=min(dp[pp],tree[i].val);
else
dp[now]+=dp[pp];
}
if(limit==1)
dp[now]=1000007;
}
int judge()
{
dfs(1,0);
if(dp[1]>m)
return 0;
return 1;
}
int solve(int l,int r)
{
int s=-1,mid;
while(l<=r)
{
mid=(l+r)/2;
flag=mid;
if(judge())
{
s=flag;
r=mid-1;
}
else
l=mid+1;
}
return s;
}
int main()
{
int a,b,c;
while(cin>>n>>m&&n+m)
{
csh();
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
build(a,b,c);
build(b,a,c);
maxx=maxx>c?maxx:c;
}
cout<<solve(1,maxx)<<endl;
}
return 0;
}