题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586

给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=10^6

dp[i]表示i节点为root的这个子树所破坏的最少费用,if(cost[i][i->son] <= limit) dp[i] += min(dp[i->son], cost[i][i->son]);

二分limit,然后把limit放到dfs中判断是不是都能切断叶子节点的联系。

 #pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
int to[N << ], Next[N << ], cost[N << ], head[N], tot, dp[N], inf = 1e6 + ; void init() {
memset(head, -, sizeof(head));
tot = ;
}
inline void add_edge(int u, int v, int c) {
Next[tot] = head[u];
to[tot] = v;
cost[tot] = c;
head[u] = tot++;
}
void dfs(int u, int p, int limit) {
dp[u] = inf;
bool inter = false;
for(int i = head[u]; ~i; i = Next[i]) {
int v = to[i];
if(v == p)
continue;
dfs(v, u, limit);
if(!inter) {
dp[u] = ;
inter = true;
}
if(cost[i] <= limit) {
dp[u] += min(dp[v], cost[i]);
} else {
dp[u] += dp[v];
}
}
}
void solve() {
int n, m, u, v, c;
while(~scanf("%d %d", &n, &m) && (n || m)) {
init();
for(int i = ; i < n; ++i) {
scanf("%d %d %d", &u, &v, &c);
add_edge(u, v, c);
add_edge(v, u, c);
}
int l = , r = ;
while(l < r) {
int mid = (l + r) >> ;
dfs(, -, mid);
if(dp[] <= m) {
r = mid;
} else {
l = mid + ;
}
}
if(r == ) {
printf("-1\n");
} else {
printf("%d\n", l);
}
}
} int main()
{
solve();
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://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

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

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

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

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

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

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

  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. Asp.Net保存session的三种方法

    C#中保存Session的三种方法及Web.Config设置 1.保存session到sql server,需要指定Sql Server服务器,这种方法因为要读写数据库最慢 <sessionSt ...

  2. android系统掉电保护

    /************************************************************************ * android系统掉电保护 * 说明: * An ...

  3. ecshop 在首页每个商品下显示已销售数量

    1.在includes/lib_goods.php文件末尾加入以下代码 function get_buy_sum($goods_id) { $sql = "select sum(goods_ ...

  4. log4net使用介绍

    log4net是一款开源的日志工具,现已挂在apache基金会下.非常简单灵活,初学者有时会发现log4参照资料配置好,但并不输出日志.这种情况,一般是没有准确定位到配置文件.可参阅第3步. 下载 下 ...

  5. UITabbar item 设置笔记

    很长一段时间都是用代码来写UITabbarController,试着用xib来写一次,但是遇到tabbar item的图标自定义的时候不知道从何入手,比如定义选定前和选定后的icon图片,这地方还是不 ...

  6. PHP String函数分类

    1.查找字符位置函数: strpos  ($str,search,[int]):    查找search在$str中的第一次位置从int开始: stripos ($str,search,[int]): ...

  7. js+css实现带缓冲效果右键弹出菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. delete之后千万要记得将指针置空,即勿重复delete!!!

    下面这段代码有什么问题吗?(Arduino上运行) class C{ public: C(){ ptr = ]; } ~C(){ if(ptr!=NULL)delete [] ptr; } void ...

  9. 用标准C编写COM(一)

    cdllbufferstruct编译器微软 目录(?)[-] 简介 COM对象和虚表 GUID QueryInterfaceAddRef and Release IClassFactory对象 打包到 ...

  10. 【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...