HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586
题意:
给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit。
思路:
对于上限limit我们可以二分查找。然后就是树形dp,看代码就可以理解的。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f; //INF如果开的比较大的话那么cost要开long long,不然会超
const int maxn=+; int tot;
int n, m;
ll cost[maxn];
int head[*maxn]; struct node
{
int v,w,next;
}e[*maxn]; void addEdge(int u, int v, int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
} void dfs(int u, int fa, int x)
{
cost[u]=;
bool flag=false;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(v==fa) continue;
flag=true;
dfs(v,u,x);
if(e[i].w>x) cost[u]+=cost[v];
else cost[u]+=min(cost[v],(ll)e[i].w);
}
if(!flag) cost[u]=INF;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(n== && m==) break;
tot=;
memset(head,-,sizeof(head));
int l=,r=;
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
r=max(r,w);
}
int ans=-;
while(l<=r)
{
int mid=(l+r)/;
dfs(,-,mid);
if(cost[]<=m) {ans=mid;r=mid-;}
else l=mid+;
}
printf("%d\n",ans);
}
return ;
}
HDU 3586 Information Disturbing(二分+树形dp)的更多相关文章
- 【题解】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.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
- HDU 3586 Information Disturbing (树形DP,二分)
题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...
- 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 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- hdu3586 Information Disturbing 【树形dp】
题目链接 hdu3586 题解 二分 + 简单的树形dp 我正有练一下dp的必要了 #include<iostream> #include<cstdio> #include&l ...
- HDU-3586 Information Disturbing(树形DP+删边)
题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的 ...
随机推荐
- 007-spring cloud gateway-GatewayAutoConfiguration核心配置-RouteDefinition初始化加载
一.RouteDefinitionLocator 在Spring-Cloud-Gateway的GatewayAutoConfiguration初始化加载中会加载RouteDefinitionLocat ...
- HTML状态消息
HTTP 状态消息 当浏览器从 web 服务器请求服务时,可能会发生错误. 以下列举了有可能会返回的一系列 HTTP 状态消息: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收 ...
- node.js 开发桌面程序, 10个令人惊讶的NodeJS开源项目
用 node-webkit 开源框架. 做企业站,杠杠地 包括电子书和支付宝系统都是node开发的,. 接收传感器发送的数据再运算...对水泵.风机.空调这些硬件进行远程控制. 细数10个令人惊讶的N ...
- 对比jQuery和AngularJS的不同思维模式
jQuery是dom驱动,AngularJS是数据驱动,这里有一篇文章阐述的非常好,建议看看 本文来自StackOverFlow上How do I “think in AngularJS” if I ...
- 下载mysql的源码包
- requirejs初体验
1.引入requirejs <script type="text/javascript" src="/js/common/require2.3.5.js" ...
- 何为仿射变换(Affine Transformation)
http://www.cnblogs.com/ghj1976/p/5199086.html 变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可 ...
- 无法在web服务器下启动调试
VS2013 编译时异常 无法在web服务器下启动调试.该Web服务器未及时响应 VS2013 编译时错误 无法在web服务器上启动调试.该Web服务器未及时响应.无法在web服务器上启动调试.该 ...
- python excel操作 练习:#生成一个excel文件,生成3个sheet,每个sheet的a1写一下sheet的名称。每个sheet有个底色
练习:#生成一个excel文件,生成3个sheet,每个sheet的a1写一下sheet的名称.每个sheet有个底色 #coding=utf-8 from openpyxl import Workb ...
- nginx的权限问题(Permission denied)解决办法
nginx的权限问题(Permission denied)解决办法 一个nginx带多个tomcat集群环境,老是报如下错误:failed (13: Permission denied) while ...