题目链接: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. HDU 1494 跑跑卡丁车

    很无爱的一道题. 题解都看得一知半解的. acm之家的题解,留着以后慢慢体会: 把这题转化为背包模型,每个%20能量算一个单位,最多有15个,如果大于5个有一个加速卡,如果大于10个有2个加速卡,如果 ...

  2. (转)UILabel的详细使用

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //初始化UIlbel并设定frame lab ...

  3. /bin/bash: [xxxx]: command not found

    /******************************************************************************** * /bin/bash: [xxxx ...

  4. (转)每天一个Linux命令(5): rm

    http://www.cnblogs.com/peida/archive/2012/10/26/2740521.html 昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和 ...

  5. phpcms还原被删除的栏目

    1.在这个目录下/caches/bakup/default导出文件category.sql 2.登录网站的数据管理页面phpmyadmin 3.导入数据库选择category.sql 4.登陆网站后台 ...

  6. 你能识别这些科技公司的真假logo吗?

    快告诉我,不止我一个眼瞎~

  7. aspose.word 查找文本并加下划线

    private Run SplitRun(Run run, int position) { Run beforeRun = (Run)run.Clone(true); beforeRun.Text = ...

  8. php 实现 jsonp 数据接口

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. Android adb shell命令大全

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...

  10. Android配置时,点击eclipse里Window->Preferences里的android选项出错

    An error has occurred when creating this preference page. 解决方法:重起eclipse