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
 

题意:

  给你n点的树,每条边有权值

  你可以任意选边切断,使得最后的所有原图中的叶子节点都不与1相连

  这会花费切断边的权值cost,切断的边中 权值最大边 w

  必须满足的是 cost不超过给定的m,且使得w尽量小

  输出这个最小的w,没有满足条件的 -1

题解:

  我们二分答案

  对于得到limit,我们在树上选边切断的时候 保持被切断的边的权值不超过它就是了,每次DP处理即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include<vector>
#include <algorithm>
using namespace std;
const int N = 1e3+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; struct edge{int to,next,w;}e[N * ];
int head[N] , t , n , m , cost, dp[N];
void add(int u,int v,int w) {e[t].to=v,e[t].w=w,e[t].next=head[u];head[u]=t++;}
void init() {t=;memset(head,-,sizeof(head));}
void dfs(int u,int fa,int limit) {
int ret = , cnt = ;
for(int i=head[u];i!=-;i=e[i].next) {
int to = e[i].to;
int value = e[i].w;
if(to == fa) continue;
cnt += ;
dfs(to,u,limit);
if(dp[to] == -) {
if(value <= limit) ret += value;
else return ;
}else {
if(value <= limit) ret += min(dp[to] , value);
else ret += dp[to];
}
}
if(cnt)dp[u] = ret;
}
int check(int limit) {
memset(dp,-,sizeof(dp));
dfs(,-,limit);
if(dp[] == -) return ;
else {
cost = dp[];
return ;
}
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
if(!n&&!m) break;
int mx = ;
init();
for(int i=;i<n;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mx = max(c,mx);
add(a,b,c);
add(b,a,c);
}
int l = , r = mx, ans = -;
while(l<=r) {
int mid = (l+r) / ;
cost = ;
int OK = check(mid);
if(OK && cost <= m) {
r = mid - ;
ans = mid;
}else {
l = mid + ;
}
}
if(ans == -) puts("-1");
else if(check(ans)&&cost <= m) cout<<ans<<endl;
else puts("-1");
}
}

HDU 3586 Information Disturbing 树形DP+二分的更多相关文章

  1. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

  2. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  3. [hdu3586]Information Disturbing树形dp+二分

    题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到l ...

  4. hdu3586 Information Disturbing 树形DP+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 题目大意:给定n个敌方据点,编号1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值c ...

  5. HDU3585 Information Disturbing 树形dp+二分

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586   题意 : 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用 ...

  6. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...

  7. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

  8. hdu 3586 Information Disturbing(树形dp + 二分)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

  9. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

随机推荐

  1. 【leetcode】Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

  2. ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)

    两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ ...

  3. Java中的try,catch(Exception e),finally及return执行顺序

    结论: ①就算之前return,finally也会执行 ②finally的计算结果不影响之前的return值 ③finally的return值一定是最后的返回结果,因此将return放入finally ...

  4. [转] Git 基础 - 打标签

    2.6 Git 基础 - 打标签 打标签 同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做.本节我们一起来学习如何列 ...

  5. Divide and conquer:Sumsets(POJ 2549)

    数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...

  6. 初次使用 VUX

    1. 因为以前没用过vux ,所以还是比较不熟练: 2.项目部署是根据git上的源码改的: 开始:将git上的项目下载或者clone到本地: 001:安装nodejs--> 002:npm in ...

  7. js中apply,call的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

  8. db2中报SQLCODE=-530, SQLSTATE=23503错误

    今天在写一个增加操作时,报错信息如下:SQLCODE=-530, SQLSTATE=23503,该sqlcode说明:“对特定的约束名指定了无效的外健值”. 项目中用的框架是SSH,新增的主对象有多个 ...

  9. 解决Idea创建maven-archetype-webapp项目无java目录的问题

    一.背景 在适用IDEA创建maven-archetype-webapp项目的时候,创建完成后发现在main文件夹下没有java源文件夹,不少小伙伴也遇到该问题,但不知道怎么解决,下面我就来分享解决步 ...

  10. windows系统查看80端口被占用的程序并结束该程序运行

    一.背景 最近系统更新以后,我在Idea中适用80端口启动项目的时候发现80端口被占用了,就查了资料看怎么找到占用80端口的程序并结束其运行,下面把解决方式共享给大家. 二.解决步骤 1.首先打开控制 ...