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)的更多相关文章

  1. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...

  2. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  3. hdu 3586 Information Disturbing(树形dp + 二分)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

  4. HDU 3586 Information Disturbing (树形DP,二分)

    题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...

  5. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

  6. HDU 3586 Information Disturbing 树形DP+二分

    Information Disturbing Problem Description   In the battlefield , an effective way to defeat enemies ...

  7. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  8. hdu3586 Information Disturbing 【树形dp】

    题目链接 hdu3586 题解 二分 + 简单的树形dp 我正有练一下dp的必要了 #include<iostream> #include<cstdio> #include&l ...

  9. HDU-3586 Information Disturbing(树形DP+删边)

    题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的 ...

随机推荐

  1. CSS 3列等高

    方法1: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"&g ...

  2. [py][mx]django使用class写views-免去判断方法的烦恼

    修改views使用class模式 类模式写views - 免去了函数模式的判断的烦恼 users/views.py from django.views.generic import View clas ...

  3. [py]监控内存并出图

    监控内存出图 先将内存数据搞到数据库 已使用内存算法 used = int(total) - int(free) - int(butffers) - int(cache) pymysql模块使用 db ...

  4. 利用javascript实现css操作

    在很多情况下,都需要对网页上元素的样式进行动态的修改.在JavaScript中提供几种方式动态的修改样式,下面将介绍方法的使用.效果.以及缺陷. 1.使用obj.className来修改样式表的类名. ...

  5. python sys.stdin、sys.stdout和sys.stderr

    学习并转载自  https://www.cnblogs.com/guyuyuan/p/6885448.html 标准输入:一般是键盘.stdin对象为解释器提供输入字符流,一般使用raw_input( ...

  6. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

  7. linux常用命令:ip 命令

    ip命令是Linux下较新的功能强大的网络配置工具. 1.命令格式: ip  [OPTIONS]  OBJECT  [COMMAND [ARGUMENTS]] 2.命令功能: ip命令用来显示或操纵L ...

  8. 你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  9. x86,x64,Any CPU区别

    https://blog.csdn.net/zuguangboy/article/details/51509670 1,即主程序(编译出来是exe文件的)是x86平台下编译的,而它所依赖的一个项目(或 ...

  10. c++中类似于java jprofiler/eclispe memoryanalysis的性能以及内存分析工具

    visual studio有自带的,可以看MSDN,不过一般来说,我们比较关注linux下的,搜了下,比较好用的应该有gprof和valgrind,先记录,可参考如下: http://blog.csd ...