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表示破坏 ...
随机推荐
- document.documentElement.scrollTop || document.body.scrollTop
如果有doctype的声明,需要用document.documentElement.scrollTop没有doctype的声明,用document.body.scrollTop
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- python to be Windows Daemon
参考:http://assback.iteye.com/blog/1731565 安装 pywin32-.win32-py2..exe #32bit pywin32-.win-amd64-py2..e ...
- winserver2008 Oracle 11g 安装
.在Windows Server2008R2上安装Oracle Database 11g Release 2,下载64位的安装程序,地址: 文件1:http://download.oracle.com ...
- 在Debian上用Bind 配置DNS服务器
1 什么是DNS 初学者可能不理解DNS到底是什么,干什么用.我是在1998年大学毕业时才听说这个词的.那时我在聊天室碰到潍坊信息港的一个网管,我恬不知耻地说我也是个网管,他说也维护DNS吗?我说,D ...
- Urllib2 总结
Urllib2 总结 介绍 Urllib2是用于获取URLs(统一资源定位符)的一个Python模块.它以urlopen函数的形式提供了非常简单的接口.能够使用各种不同的协议来获取网址.它还提供一个稍 ...
- 针对Xcode的警告忽略消除处理
一.问题描述 html代码如下 <html> <head> <meta charset="utf-8"/> <title>我的网页& ...
- windows server 2008 配置安装AD 域控制器
工作需要,搞起AD域来,具体配置如下: 配置环境 Windows版本:Windows Server 2008 R2 Enterprise Service Pack 1 系统类型: 64 位操作系统 配 ...
- jquery $(document).ready() 与window.onload的异同
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 ...
- struts2截取字符串
<struts:if test="null!=pushAd&&pushAd.length()>14"> <struts:property v ...