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. Domino代理运行问题

    当Server出现“operation is disallowed in this session”此命令时为代理权限问题,修改后即可正常运行代理.

  2. 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容

    问题的产生 在写JS的过程中,为了调试我们常常会写很多 console.log.console.info.console.group.console.warn.console.error代码来查看JS ...

  3. 十天精通CSS3(1)

    什么是CSS3? CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能. 目前主流浏览器chrome.safari.firefox.opera.甚至360都已经支 ...

  4. csv文件的读写

    # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. "&quo ...

  5. 解决PHPStorm经常卡顿现象 调整内存限制

    https://www.jisec.com/other/451.html 为什么调整内存? 最近发现PHPstorm在打开一些大点的js, html文件时,会非常的卡顿,这个主要的原因是因为设置的内存 ...

  6. Session实例

    Session常用方法(一) session对象用来保存一些在与每个用户回话期间需要保存的数据信息,这样就方便了回话期间的一些处理程序.如可以用session变量记住用户的用户名,以后就不必在其他的网 ...

  7. MySQL读写分离-简单思考

    本文图片资源均来自互联网,没有干货,只是提供一种简单的思路. 基础原理 两台MySQL机器一个主,一个从实现数据实时同步比较简单,代码层面无需任何修改,添加一台机器简单配置配置即可,但是MySQL数据 ...

  8. css中外边距

    1.内部元素设置margin等,父元素高度不能适应 .classA { height: 200px; background-color: cornflowerblue; overflow: hidde ...

  9. 浅谈Java中的==和equals

    引言 最近在看TIJ,看到==和equals相关内容,今天就来简单的总结下. 关系操作符== 书中对关系操作符的描述是这样的:"关系操作符生成的是一个boolean结果,它们计算的是操作数的 ...

  10. mvc actionresult返回各种文件

    public ActionResult ReviewFile(string folderName, string fileBasename, string extendName) { //以后根据后缀 ...