题目:
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
一棵树,切断一些边,使得叶子节点到根节点不连通的,但是花费有个限制,需要你求出最大权得最小值 。最大值最小化需要二分,但是二分后得,检验标准是什么?那就是建完以后得花费,如果标准设得猜得高了,就会超出限制,就需要缩小标准,树形dp。
dp[i]表示当前节点的最小花费。
决策:当前得边到底剪不剪,取决于他和标准得大小,所以状态转移:
void tree_dp(int node,int f)
{
dp[node] = 0;bool leaf = 1;
for(int i = 0;i < (int)s[node].size();i ++)
{
int &son = s[node][i];
if(son == f)continue;
leaf = 0;
tree_dp(son,node);
if(edge[node][son] <= limit) dp[node] += min(dp[son],edge[node][son]);
else dp[node] += dp[son];
}
if(leaf) dp[node] = INF;//叶子节点为无穷大。
}
完整代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1005;
const int INF = 1000005;
vector<int>s[maxn];
int edge[maxn][maxn];
int n,m,limit;
int dp[maxn];
void tree_dp(int node,int f)
{
dp[node] = 0;bool leaf = 1;
for(int i = 0;i < (int)s[node].size();i ++)
{
int &son = s[node][i];
if(son == f)continue;
leaf = 0;
tree_dp(son,node);
if(edge[node][son] <= limit) dp[node] += min(dp[son],edge[node][son]);
else dp[node] += dp[son];
}
if(leaf) dp[node] = INF;
}
bool check()
{
tree_dp(1,0);
if(dp[1] > m)return 0;
else return 1;
}
int bin_search(int l,int r)
{
int ans = -1;
while(l <= r)
{
int mid = (l + r) / 2;
limit = mid;
if(check())
{
ans = limit;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)==2&&(n+m))
{
memset(s,0,sizeof(s));
memset(edge ,0,sizeof (edge));
memset(dp,0,sizeof dp);
int a,b,w,l = 1,r = 0;
for(int i = 0;i < n-1;i ++)
{
scanf("%d%d%d",&a,&b,&w);
edge[a][b] = edge[b][a] = w;
s[a].push_back(b);
s[b].push_back(a);
r = max(r,w);
}
printf("%d\n",bin_search(l,r));
}
return 0;
}