[hdu3586]Information Disturbing树形dp+二分
题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束。
解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可。
dp数组需要开到ll,如果用设inf的解法。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
const int inf=0x3f3f3f3f;
struct edge{
int to;
int nxt;
int w;
}e[maxn<<];
ll cnt,head[];
ll dp[];
int n,m;
inline int read(){
char k=;char ls;ls=getchar();for(;ls<''||ls>'';k=ls,ls=getchar());
int x=;for(;ls>=''&&ls<='';ls=getchar())x=(x<<)+(x<<)+ls-'';
if(k=='-')x=-x;return x;
}
void add_edge(int u,int v,int w){
e[cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
} void dfs(int u,int fa,int val){//还可以通过设立flag来确定叶子节点
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u,val);
ll tmp=e[i].w>val?inf:e[i].w;
if(dp[v]==) dp[u]+=tmp;
else dp[u]+=min(tmp,dp[v]);
}
} int erfen(int l,int r){
bool flag=false;
while(l<r){
int mid=(l+r)>>;
memset(dp,,sizeof dp);
dfs(,-,mid);
if(dp[]<=m) r=mid,flag=true;
else l=mid+;
}
if(flag) return r;
else return -;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
cnt=;
memset(head,-,sizeof head);
int a,b,c;
for(int i=;i<n-;i++){
a=read();b=read();c=read();
add_edge(a,b,c);
add_edge(b,a,c);
}
int ans=erfen(,);
printf("%d\n",ans);
}
}
[hdu3586]Information Disturbing树形dp+二分的更多相关文章
- hdu3586 Information Disturbing 树形DP+二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 题目大意:给定n个敌方据点,编号1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值c ...
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
- HDU 3586 Information Disturbing 树形DP+二分
Information Disturbing Problem Description In the battlefield , an effective way to defeat enemies ...
- [HDU3586]Information Disturbing(DP + 二分)
传送门 题意:给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit 二分答案,再 DP,看看最终结果是 ...
- HDU3585 Information Disturbing 树形dp+二分
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意 : 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- hdu3586 Information Disturbing[二分答案+树形DP]
给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
随机推荐
- ASP.NET动态网站制作(1)--html
前言:正式上课的第一课,讲的是前端部分的最基础内容:html. 前端:html,css,js 数据库:sql server 动态部分:.net,c#... IIS(Internet Informati ...
- linux 设置静态IP方法
本系统使用 linux redhat 7.2 1. 修改ip vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 2. 修改数据项如下 3. ...
- python 基础 8.2 编译正则对象
#/usr/bin/python #coding=utf-8 #@Time :2017/11/14 9:55 #@Auther :liuzhenchuan #@File :编译正则对象.py ...
- 【C#图解】PictureBox.SizeMode 属性(转)
PictureBoxSizeMode.Normal: 默认情况下,在 Normal 模式中,Image 置于 PictureBox 的左上角,凡是因过大而不适合 PictureBox 的任何图像部分都 ...
- php soap使用示例
soap_client.php <?php try { $client = new SoapClient( null, array('location' =>"http://lo ...
- python cookbook第三版学习笔记二:字典
一般来说字典中是一个键对应一个单值的映射,如果想一个键值映射多个值,那么就需要将这些值放到另外的容器中,比如列表或者集合. 比如d={'a':[1,2]} Collections中的defaultdi ...
- JSP中的内容布局
参考 :https://stackoverflow.com/questions/10529963/what-is-the-best-way-to-create-jsp-layout-template ...
- Oracle数据库之SQL基础和分支循环
一.SQL基础语言 DECLARE --声明 a ); --变量或对象 BEGIN a:='小明';-- := 表示给一个变量赋值 dbms_output.put_line(a); --输出用 dbm ...
- 细说后端模板渲染、客户端渲染、node 中间层、服务器端渲染(ssr)
细说后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr) 前端与后端渲染方式的发展大致经历了这样几个阶段:后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr). 1. 后端 ...
- appium不支持Android7.0系统设备解决办法
1. 找到appium的安装目录下的adb.js文件. 2. 打开adb.js,手动修改该文件下的内容: Adb.prototype.getPIDsByName=function(name,cb){ ...