HDU 3586 树形dp
Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2856 Accepted Submission(s): 1022
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.
//从下到上处理当到了x时的最小花费是切断x与儿子节点y之间的边和切断y子树中的
//某几条边中小的值,如此更新节点值最后1点的花费就是答案;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=;
const int maxn=;
int n,m,head[maxn],tol,val[maxn],vis[maxn];
struct Edge{
int to,w,next;
}edge[maxn*];
void Add(int x,int y,int z){
edge[tol].to=y;
edge[tol].w=z;
edge[tol].next=head[x];
head[x]=tol++;
}
void dfs(int x,int fa,int mid){
val[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y==fa) continue;
dfs(y,x,mid);
if(edge[i].w<=mid)
val[x]+=min(val[y],edge[i].w);
else val[x]+=val[y];
}
if(val[x]==) val[x]=inf;
}
int main()
{
while(scanf("%d%d",&n,&m)&&(n+m)){
memset(head,-,sizeof(head));
tol=;
int l=inf,r=,mid;
for(int i=;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Add(x,y,z);Add(y,x,z);
l=min(l,z);
r=max(r,z);
}
int ans=-;
while(l<=r){
mid=(l+r)>>;
dfs(,,mid);
if(val[]<=m){
ans=mid;
r=mid-;
}
else l=mid+;
}
printf("%d\n",ans);
}
return ;
}
HDU 3586 树形dp的更多相关文章
- hdu 3586 树形dp+二分
题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限lim ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- codevs 1380/HDU 1520 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...
- HDU 5834 [树形dp]
/* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...
- hdu 4267 树形DP
思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...
随机推荐
- Sharepoint 2013与Sharepoint 2016的功能对比
开发人员功能 SharePoint Foundation 2013 SharePoint Server 2013 Standard CAL SharePoint Server 2013 Enterpr ...
- Python3 标准库:calendar,time
1.calendar import calendar print(calendar.month(2008,8)) #某个月 print(calendar.calendar(2008)) #某年 pri ...
- from module import 和 import 的区别
最近在用codecademy学python,遇到一些题目错误,小小记录一下 如from math import sqrt是把sqrt作为本文件的方法导入进来了,使用的时候只需要直接调用sqrt. 而如 ...
- HTTP 请求头 & 响应头
HTTP请求头概述 HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.大多数请求头并不是必需的, 但Co ...
- 链表相加(Add Two Numbers)
描述: 给定两个非空的链表,表示两个非负整数.数字以相反的顺序存储,每个节点包含一个数字.添加两个数字并将其作为链表返回. 您可以假设两个数字不包含任何前导零,除了数字0本身. 输入:(2 - > ...
- ZigBee设备入网流程之关联方式
ZigBee设备入网流程 ZigBee设备入网有关联方式和直接方式两种,我所熟悉的是关联方式,这也是最常用的方式. 关联方式 step1 设备发出Beacon Request 设备会在预先设置的几个信 ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-4
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 完成程序副界面的设计与信息的输入统计 明天计划完成的工作: 日期等细致信息的处理 工作中遇到的困难: 对微信小程序开发的代码构成有了一些了解 ...
- Firefox火狐浏览器 修改默认搜索引擎
如图:
- project之chrome.exe
查看chrome.exe的以来文件可以得到下面这个列面,大部分是在%systemroot%/system32下面的系统dll文件,只有两个是chromium自己生成的:base.dll, conten ...
- perf打印调用栈的过程
perf_prepare_sample-->perf_callchain-->get_perf_callchain 上面的调用栈会使用 perf_event_output--> 0x ...