HDU 3586 Information Disturbing(树形DP)

来源:互联网 发布:如何通过网络发送短信 编辑:程序博客网 时间:2024/05/23 18:02

题意给出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 longint 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);elsedp[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;elsel=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 51 3 21 4 33 5 54 2 60 0
 

Sample Output

3
 



0 0
原创粉丝点击