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.


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.


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.

Sample Input

5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int f=1000005;
int n,m;
int dp[1005];
int head[1005];
int minshu;
int ans;
struct shu
{
int v;
int next;
int va;
}tr[2005];
void add(int q,int w,int v)
{
tr[ans].v=w;
tr[ans].va=v;
tr[ans].next=head[q];
head[q]=ans++;

}
void dfs(int x,int pa,int k)
{

dp[x]=0;
int p=0;
for(int i=head[x];i!=-1;i=tr[i].next)
{
int son=tr[i].v;
if(son!=pa)
{
p=1;
int w=tr[i].va;
dfs(son,x,k);
if(w>k)dp[x]+=dp[son];
else dp[x]+=min(w,dp[son]);

}
}
if(!p)dp[x]=f;

}
int main()
{

while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
memset(head,-1,sizeof(head));
ans=0;
int ma=0;
for(int i=1;i<n;i++)
{
int q,w,v;
scanf("%d%d%d",&q,&w,&v);
add(q,w,v);
add(w,q,v);
ma=max(ma,v);
}

int l=1,r=ma,mid;
ans=f;
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==f)printf("-1\n");
else printf("%d\n",ans);

}
return 0;
}


Sample Output

3




题目大概:

一棵树,每条边,都有成本,使得1和其他点没有联系,需要切断边,在不超过总成本的前提下,最低的成本上线是多少。

思路:

可以枚举所有的成本,找出最小的下线成本,用二分枚举,用dfs遍历一边树,看是否符合不超过总成本。


代码: