Information Disturbing
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.
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
Sample Output
3
Author
alpc86
Source
2010 ACM-ICPC Multi-University Training Contest(15)——Host by NUDT
Recommend
zhouzeyong
/*
2017/3/14
题意:
给一棵n个节点的树,节点编号为1~n,根节点为1。每条边有权值,砍掉一条边要花费cost(w)
要砍掉一些边, 使得每个叶子节点无法走到根节点。
要求砍掉花费总和不能超过m的情况下,让每条边花费上限尽量低
思路:
二分可以砍的边的权值上限,然后树形dp
f[i]: 表示让i子树所有的叶子节点无法到达i点的最小花费
f[i] = sum { min(w(i,v), f[v]) | v是i的儿子节点 }
而这题有一个上限权值,即权值大于上限的就不能去砍。
所以上面的转移要改一下
if (w(i, v) > 上限) f[i] += f[v];
else f[i] += min(w(i,v), f[v]);
然后,如果f[1]<=m,那么这个上限就符合条件
这一题,如果用INF初始化的话,那么INF一定要大于m的最大值1000000
**注意:**最初将INF 定义为: #define INF 10000000 结果编译报错:if(ans != INF ) 这句代码有问题。一脸懵。。。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1010;
const int INF = 1000010;
struct edge
{
int v,w,next;
}edges[2*maxn];
int head[maxn],e,n,m,mid;
int cost[maxn];
void addedge(int u,int v,int w)
{
edges[e].v = v;
edges[e].w = w;
edges[e].next = head[u];
head[u] = e++;
}
void dfs(int u,int f)
{
if(edges[head[u]].v == f) //若为叶子节点
{
cost[u] = INF;
return ;
}
cost[u] = 0;
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
int w = edges[i].w;
if(v == f) continue;
dfs(v,u);
if(w > mid) cost[u] += cost[v]; //若(u,v)边不可减
else cost[u] += min(cost[v],w); //若(u,v)边可减
}
}
int main()
{
int u,v,w;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n == 0 && m == 0) return 0;
int high = 0;
int low = 0;
memset(head,-1,sizeof(head));
e = 0;
for(int i=0;i<n-1;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
if(w > high) high = w;
}
int ans = INF;
while(low <= high)
{
mid = (high + low) / 2;
dfs(1,0);
if(cost[1] <= m)
{
high = mid - 1;
ans = min(ans,mid);
}
else low = mid + 1;
}
if (ans != INF)
printf("%d\n", ans);
else
puts("-1");
}
return 0;
}