HDU 3586 Information Disturbing(树形DP)

12 篇文章 1 订阅

题意给出n个士兵,其中1号为指挥官,关系为树状结构,叶子为先锋,现在要在总花费小于m的情况切断所有的先锋与指挥官的联系,问最大的限制最小为多少

思路:最大值最小的问题..显然二分,然后对于每一个值,树形DP判定是否可行dp[i]表示要切断以i为根的其它所有子树的最小代价。其中设定叶子结点的代价为无穷大那么对于某一个非叶子结点,要切断一棵子树就有两种选择,切断以孩子为根的子树或者切断根与孩子的边。如果根与孩子的边大于限制,那就取无穷大。最后判断1号结点的总花费是否小于等于m

注意:无穷大不要取太大,否则会连续相加溢出



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100005
#define inf 1<<20
#define LL long long
int cas=1,T;
int n,m;
struct Edge
{
	int v,w;
	Edge(int vv,int ww):v(vv),w(ww){}
};
vector<Edge>e[maxn];
int dp[maxn];
void dfs(int u,int fa,int limit)
{
    int flag = 0;
	dp[u]=0;
	for (int i = 0;i<e[u].size();i++)
	{
		int v = e[u][i].v;
		if (v==fa)
			continue;
		flag=1;
		dfs(v,u,limit);
		if (e[u][i].w<=limit)
			dp[u]+=min(dp[v],e[u][i].w);
		else
			dp[u]+=dp[v];
	}
	if (!flag)
		dp[u]=inf;
}
bool check(int mid)
{
	memset(dp,0,sizeof(dp));
	dfs(1,-1,mid);
	if (dp[1]<=m)
		return true;
	return false;
}
int main()
{
	while (scanf("%d%d",&n,&m)!=EOF && (n+m))
	{
		for (int i = 0;i<=n;i++)
			e[i].clear();
		int maxw=0;
        for (int i = 1;i<n;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			e[u].push_back(Edge(v,w));
		    e[v].push_back(Edge(u,w));
			maxw=max(maxw,w);
		}
        int l = 1,r=maxw;
		int ans = -1;
		while (l<=r)
		{
			int mid = (l+r)/2;
			if (check(mid))
				r=mid-1,ans=mid;
			else
				l=mid+1;
		}
		printf("%d\n",ans);
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

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
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值