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. fstream的使用方法介绍

    转载自:  fstream的使用方法介绍 - saga's blog - C++博客 http://www.cppblog.com/saga/archive/2007/06/19/26652.html ...

  2. C#之基类及接口

    Component类: Component是公共语言运行库中按引用封送的所有组件的基类.Component提供IComponent接口的基实现并启用应用程序之间的对象共享. https://msdn. ...

  3. mysql的innodb中事务日志ib_logfile

    mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...

  4. (转)大数据时代下的SQL Server第三方负载均衡方案----Moebius测试

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...

  5. Windows下用python编写简单GUI程序的方法

    Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py #!C:\Python27\python.exe ...

  6. Jquery 实现 “下次自动登录” 记住用户名密码功能

    转载自:http://blog.csdn.net/aspnet_lyc/article/details/12030039?utm_source=tuicool&utm_medium=refer ...

  7. 【python】sqlite使用

    官方文档:https://docs.python.org/2/library/sqlite3.html sqlite教程:http://www.runoob.com/sqlite/sqlite-del ...

  8. yii 多模板

    main.php: //替换所有模板 //加载文件名为first的模板 //       'theme'=>'theme1', 'components'=>array(           ...

  9. .net 获取https页面的信息 在iis7.5服务器上不管用

    让我纠结了一天多的问题,给大家看下,有相同情况的可以不用浪费时间了,本人当时找了好半天都没找到什么有用的信息,项目在本地没有问题,但部署在服务器后,获取不到https页面的信息,加入下面的代码就可以了 ...

  10. python基础——类和实例

    python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...