[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.每条边有权值,砍掉一条边要花费 ...
随机推荐
- python 基础2.5 循环中continue与breake用法
示例1: #循环退出,break continue.break 跳出最外层循环:continue跳出内层循环 #当 i=5时,通过continue 跳出当前if循环,不在执行if循环中后边的语句.i= ...
- input file 选择Excel文件 相关操作
1.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFo ...
- Netty聊天室-源码
目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...
- Locality-sensitive hashing Pr[m(Si) = m(Sj )] = E[JSˆ (Si, Sj )] = JS(Si, Sj )
A hash function that maps names to integers from 0 to 15. There is a collision between keys "Jo ...
- Swift 学习笔记 (类和结构体)
类和结构体是一种多功能且灵活的构造体.通过使用与现存常量 变量 函数完全相同的语法来在类和结构体中定义属性和方法以添加功能. Swift中不需要你为自定义的类和结构体创建独立的结构和实现文件.在Swi ...
- win7怎么设置打印机共享
一.设置好家庭组,让客户机加入家庭组 二.对服务机的打印机进行共享设置,如果保存不成功请在计算机服务那里打开防火墙 三.1.开启guest用户,具体操作:我的电脑右击---管理---本地用户和组--开 ...
- java多线程---基础
一, java多线程----线程与进程 进程: 程序(任务)的执行过程,拥有资源(共享内存,共享资源)和线程(一个或者多个,至少一个). 例如:打开任务管理器,qq,chrome,都属于进程. 线程 ...
- leetcode 890. Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- :style动态设置属性
前段时间做页面时需要动态设置背景图片,每一种框架都会遇见类似的需求,特记录下来,以免不时之需: <!DOCTYPE html> <html> <head> < ...
- Android开发学习之三——第一个Android程序
下面我们建立第一个Android程序. 打开Eclipse,开始如下步骤: 1.File ==> New ==> Android Application Project 出现如下窗口: 2 ...