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
题意与HDU 3452 Bonsai,基本类似,不过这题稍微复杂了点,要求的是切下来的最大的边的最小长度。
所以要二分答案验证。
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
using namespace std;
const int Maxn = 1005;
vector<int> tree[Maxn];
int n, x, y, z, dp[Maxn], m, c[Maxn][Maxn], maxn, q, h;
void work(int x, int fa, int ans, int limit)
{
int tot = 0;
for (int i = 0; i < tree[x].size(); i++)
if (tree[x][i] != fa)
{
int y = tree[x][i];
work(y, x, c[x][y],limit);
tot += dp[y];
}
if (ans>limit) ans = 1 << 20;
dp[x] = min(tot ? min(tot, ans) : ans, 1 << 20);
}
int main()
{
while (~scanf("%d%d", &n, &m), n + m)
{
maxn = 0;
for (int i = 0; i <= n; i++) tree[i].clear();
for (int i = 1; i < n; i++)
{
scanf("%d%d%d", &x, &y, &z);
c[x][y] = c[y][x] = z;
maxn = max(maxn, z);
tree[x].push_back(y);
tree[y].push_back(x);
}
int q = 1, h = maxn, mid;
while (q < h)
{
mid = (q + h) >> 1;
work(1, 1, 1 << 30, mid);
if (dp[1] <= m) h = mid;
else q = mid + 1;
}
work(1, 1, 1 << 30, q);
if (dp[1]>m) cout << -1 << endl;
else cout << q << endl;
}
}