[HDU3586]Information Disturbing(DP + 二分)
题意:给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit
二分答案,再 DP,看看最终结果是否小于 m
——代码
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1001
#define max(x, y) ((x) > (y) ? (x) : (y)) int n, m, cnt, ans;
int a[N], sum[N], head[N], to[N << ], val[N << ], next[N << ];
bool vis[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dfs(int u, int x)
{
int i, v;
vis[u] = ;
for(i = head[u]; i ^ -; i = next[i])
{
v = to[i];
if(!vis[v])
{
dfs(v, x);
if(val[i] <= x)
{
if(val[i] < sum[v]) sum[u] += val[i];
else sum[u] += sum[v];
}
else sum[u] += sum[v];
}
}
} inline bool check(int x)
{
memset(vis, , sizeof(vis));
memset(sum, , sizeof(sum));
for(int i = ; i <= n; i++)
if(a[i] == )
sum[i] = m + ;
dfs(, x);
return sum[] <= m;
} int main()
{
int i, x, y, z, mid;
while()
{
cnt = ;
n = read();
m = read();
if(!n && !m) break;
memset(a, , sizeof(a));
memset(head, -, sizeof(head));
for(i = ; i < n; i++)
{
a[x = read()]++;
a[y = read()]++;
z = read();
add(x, y, z);
add(y, x, z);
}
x = , y = m, ans = -;
while(x <= y)
{
mid = (x + y) >> ;
if(check(mid)) ans = mid, y = mid - ;
else x = mid + ;
}
printf("%d\n", ans);
}
return ;
}
[HDU3586]Information Disturbing(DP + 二分)的更多相关文章
- hdu3586 Information Disturbing 树形DP+二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 题目大意:给定n个敌方据点,编号1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值c ...
- [hdu3586]Information Disturbing树形dp+二分
题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到l ...
- hdu3586 Information Disturbing[二分答案+树形DP]
给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足 ...
- hdu3586 Information Disturbing 【树形dp】
题目链接 hdu3586 题解 二分 + 简单的树形dp 我正有练一下dp的必要了 #include<iostream> #include<cstdio> #include&l ...
- HDU-3586 Information Disturbing(树形DP+删边)
题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的 ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- HDU3586 Information Disturbing(树形DP)
题目大概说一棵树有边权,要删掉一些边,使叶子到达不了树根1且删掉边的权和小于等于m,问删掉边中最大权的最小值能是多少. 考虑问题规模,与转移的时间复杂度,用这么个状态dp: dp[u][k]表示在u结 ...
- HDU 3586 Information Disturbing 树形DP+二分
Information Disturbing Problem Description In the battlefield , an effective way to defeat enemies ...
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
随机推荐
- 洛谷 P2365 任务安排【dp】
其实是可以斜率优化的但是没啥必要 设st为花费时间的前缀和,sf为Fi的前缀和,f[i]为分组到i的最小花费 然后枚举j转移,考虑每次转移都是把j到i分为一组这样意味着j及之后的都要增加s的时间,同时 ...
- hdu2031
http://acm.hdu.edu.cn/showproblem.php?pid=2031 #include<stdio.h> #include<math.h> #inclu ...
- 笔试题的各种trick
%x 默认去掉前导零 #include<stdint.h> #include<stdio.h> union X { int32_t a; struct ...
- duilib属性
原文转载自:http://blog.csdn.net/lixiang987654321/article/details/45008441 这里我想讲解一下duilib中的一些属性的理解,当然这是一篇永 ...
- Android RecyclerView 滑动时图片加载的优化
RecyclerView 滑动时的优化处理 在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.resume.这里为了避免重复设置增加开销,设置了一个标志变量 ...
- Python安装笔记
1.教程
- MyEclipse 快捷键方法
MyEclipse企业级工作平台(My Eclipse Enterprise Workbench,简称MyEclipse)是对EclipseIDE的扩展,利用它可以在数据库和J2EE的开发.发布,以及 ...
- 6.14 提取第n个分割的子串
问题:从字符串中提取出一个指定的.由分割符隔开的子字符串.create view v as select 'mo,larry,curly' as namefrom t1union allselect ...
- STL容器的排序
STL容器的排序,支持随机访问的容器vector,deque,string没有sort成员,可调用std::sort排序:list排序调用自带的list::sort. 下面是std::sort函数,有 ...
- jenkins自动部署测试环境
构建脚本如下: echo "当前目录":$(pwd)echo "当前时间":$(date +%Y-%m-%d_%H:%M)find ./ -type f -na ...