您的位置:首页 > 其它

HDU 3586 Information Disturbing 树形DP+二分

2016-04-08 11:54 176 查看

题目描述:

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


题目分析:

题目大意:n个点,1点为根结点,每条边有一个权表示将这个边去除需要多少,你的任务是分割边,将所有叶子节点与根结点不连通,但是要保证权之和不能超过m,输出这些被拆的边权的最大值。

二分查找所有权的和,深搜更新dp值。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 1100000;//INF值只需比m稍大即可太大反而会溢出
const int MAXN = 1010;

struct node
{
int u,v,w,next;
}mp[MAXN<<2];

int dp[MAXN];//dp[i]为i为父节点下删除叶子节点的最小值
int maxx;
int mid;
int head[MAXN],id;
void init()
{
memset(head,-1,sizeof(head));
id=0;
}

void addedge(int u,int v,int w)
{
mp[id].u=u;
mp[id].v=v;
mp[id].w=w;
mp[id].next=head[u];
head[u]=id++;
}

void dfs(int u,int pre)
{
bool flag=false;
for(int i=head[u]; i!=-1; i=mp[i].next)
{
int v=mp[i].v;
if (v==pre) continue;//叶子节点
flag=true;
dfs(v,u);
}
if (!flag) {dp[u]=INF; return;}
for(int i=head[u]; i!=-1; i=mp[i].next)
{
int w=mp[i].w;
int v=mp[i].v;
if(v==pre)continue;
if(w<=mid)//符合条件
dp[u]+=min(dp[v],w);
else//不符合条件
dp[u]+=min(dp[v],INF);
}
}

int main()
{
int m,n;
while(~scanf("%d%d",&n,&m) && (n || m))
{
init();
maxx=0;
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
maxx+=w;//所有权的和
}
int l=0,r=maxx;
bool flag=false;
while(l<r)
{
mid=(l+r)>>1;
memset(dp,0,sizeof(dp));
dfs(1,-1);
if (dp[1]<=m)
{
flag=true;//找到符合条件的值
r=mid;
}
else l=mid+1;
}
if (!flag) printf("-1\n");
else  printf("%d\n",r);//最后r值为二分结果
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: