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. 了解Linux的进程与线程

    了解Linux的进程与线程 http://timyang.net/linux/linux-process/ 上周碰到部署在真实服务器上某个应用CPU占用过高的问题,虽然经过tuning, 问题貌似已经 ...

  2. 通过Tacker将NFV引入OpenStack

    14年的这个时候,我们还在OpenStack社区中为NFV是否属于OpenStack而争论不休.如今这一争议已经被解决了.OpenStack已经成为NFV讨论中的重要部分,正如下面的ETSI MANO ...

  3. 虚拟机VMware的网络设置出了问题会导致很多莫名的错误

    邪门地CentOS内软件安装失败问题:Xshell与虚拟机的各种连接失败:CentOS下eth0没显示ip地址. 这些原因竟然是一个,虚拟机VMware的网络设置出了问题.     恢复初始设置即可.

  4. matlab做曲线拟合

    python 做曲线拟合 https://blog.csdn.net/qq_16583687/article/details/72723708 matlab做拟合函数,可以在命令行输入:数据x,数据y ...

  5. svn 常见问题记录

    One or more files are in a conflicted state 情景:A组员新增文件并提交,B组员更新出现如下图情况. 解决方案:直接拷贝到B组员工作区.

  6. jQ live用法

    我们给元素绑定单击事件.用得最多的都是$("#id").click(function(){}); 但我们动态添加的元素.这样绑定是不行的.必须借助live  $("#id ...

  7. VMware coding Challenge:Date of Weekday

    这道题再次证明了这种细节的题目,画个图容易搞清楚 import java.util.Scanner; public class Solution2 { static int DateOfWeekday ...

  8. win10下的iis的配置(服务于asp.net)

    win10下的iis的配置和win7下的是类似的. 1.右键开始,打开控制面板,进入卸载程序中,勾上如下图所示的项目,即可装上iis. 这里写图片描述 2.重启后搜索iis,进入iis配置中.点击网站 ...

  9. cc150 --链表分割

    题目描述 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针.注意:分割以后 ...

  10. ngxin开启rewrite伪静态

    1.编辑nginx配置文件 vi /usr/local/nginx/conf/nginx.conf #include enable-php.conf; include enable-php-pathi ...