HDU 3586 Information Disturbing 树形DP+二分
Information Disturbing
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.
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.
If there is no way to finish the task, output -1.
1 3 2
1 4 3
3 5 5
4 2 6
0 0
题意:
给你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+二分的更多相关文章
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- [hdu3586]Information Disturbing树形dp+二分
题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到l ...
- hdu3586 Information Disturbing 树形DP+二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 题目大意:给定n个敌方据点,编号1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值c ...
- HDU3585 Information Disturbing 树形dp+二分
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意 : 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用 ...
- 【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
- HDU 3586 Information Disturbing (二分+树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
随机推荐
- poj 2251
http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...
- adb操作命令详解及大全
adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...
- Spring MVC配置静态资源的正常访问
SpringMVC如果过滤器过滤范围配置了/或者/*,那么框架会过滤所有请求,包括自己写的请求和静态资源请求,这样静态资源就不能正常加载,包括js文件.css文件.图片资源访问的时候都会出现404页面 ...
- winrt 页面进入动画
private async void DoAnimistion(){Storyboard storyboard = new Storyboard(); using (IEnumerator<De ...
- 后台session过期,tomcat重启,自动跳转页面js写法
if (window != top) { //top.location.href = location.href;//因为系统分为普通用户和后台,所以暂时写死 if(top.location.href ...
- yii和php的一些细节
yii index.php ini_set("display_errors", 1); iconv这个函数不能直接用 需要检测!!! function gb2312_utf8( ...
- September 11th 2016 Week 38th Sunday
Nothing happens unless first a dream. 一切始于梦想. When everything seems to be going against you, remembe ...
- Javascript异步编程方法总结
现在我们有三个函数,f1, f2, f3 按正常的思路我们会这样写代码: function f1 (){}; function f2 (){}; function f3 (){}; //在这里调用函数 ...
- stm32——NFC芯片--PN532的使用
stm32——NFC芯片--PN532的使用 一.NFC简介 NFC(Near Field Communication)近场通信,是一种短距高频的无线电技术,在13.56MHz频率运行于20厘米距离内 ...
- Git 、 Cocoapods常用命令
Git常用命令 1.添加文件 git add xxx 2.提交更新到本地 git commit -m 'local-repo' 3.提交更新 git push master ...