hdu3586 Information Disturbing 【树形dp】
题目链接
题解
二分 + 简单的树形dp
我正有练一下dp的必要了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 1005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne = 2;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
ed[ne] = (EDGE){u,h[v],w}; h[v] = ne++;
}
int fa[maxn],w[maxn];
int n,m,lim;
void DFS(int u){
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; w[to] = ed[k].w;
DFS(to);
}
}
LL f[maxn][2];
void dfs(int u){
if (w[u] <= lim) f[u][1] = w[u];
else f[u][1] = INF;
f[u][0] = 0;
int cnt = 0;
Redge(u) if ((to = ed[k].to) != fa[u]){
dfs(to); cnt++;
f[u][0] += min(f[to][0],f[to][1]);
}
if (!cnt) f[u][0] = INF;
}
bool check(int x){
lim = x;
dfs(1);
return f[1][0] <= m;
}
int main(){
while ((n = read()) && (m = read())){
memset(h,0,sizeof(h)); ne = 2;
int a,b,w;
for (int i = 1; i < n; i++){
a = read(); b = read(); w = read();
build(a,b,w);
}
DFS(1);
int l = 1,r = m,mid;
while (l < r){
mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (check(l)) printf("%d\n",l);
else puts("-1");
}
return 0;
}
hdu3586 Information Disturbing 【树形dp】的更多相关文章
- [hdu3586]Information Disturbing树形dp+二分
题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到l ...
- hdu3586 Information Disturbing 树形DP+二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 题目大意:给定n个敌方据点,编号1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值c ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
- HDU 3586 Information Disturbing 树形DP+二分
Information Disturbing Problem Description In the battlefield , an effective way to defeat enemies ...
- [HDU3586]Information Disturbing(DP + 二分)
传送门 题意:给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit 二分答案,再 DP,看看最终结果是 ...
- HDU3585 Information Disturbing 树形dp+二分
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意 : 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用 ...
- hdu3586 Information Disturbing[二分答案+树形DP]
给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足 ...
- HDU3586 Information Disturbing(树形DP)
题目大概说一棵树有边权,要删掉一些边,使叶子到达不了树根1且删掉边的权和小于等于m,问删掉边中最大权的最小值能是多少. 考虑问题规模,与转移的时间复杂度,用这么个状态dp: dp[u][k]表示在u结 ...
随机推荐
- 【Python全栈-CSS】background背景
background背景 一.背景图片 background-image: url("img/num.png"); background-position-x: -200px ; ...
- java设计模式——建造者模式
一. 定义与类型 定义:将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示 用户只需制定需要建造的类型就可以得到它们,建造过程以及细节不需要知道 类型:创建型 建造者模式与工厂模 ...
- centos 6 安装VMware Tools
开启虚拟机的centos系统, 在虚拟机工具栏点击 “虚拟机”=>VMwareTools安装, centos系统内的桌面会有一个VMware Tools的驱动光驱, 双击打开后,有一个tar. ...
- JavaScript 交换两个变量的值
方法一 let a = "a", b = "b"; console.log(a, b); let t = a; a = b; b = t; console.lo ...
- Qt之QThread随记
这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...
- python笔记-tuple元组的方法
#!/usr/bin/env python #-*- coding:utf-8 -*- # 创建空元组 tuple1 = () print(tuple) # 创建带有元素的元组 # 元组中的类型可以不 ...
- Linux 系统性能
Linux:PS命令详解与使用 要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,ps命令就是最基本进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态.进程 ...
- tp5 修改自带success或error跳转模板页面
tp5 修改自带success或error跳转模板页面 我们在使用tp5或者tp3.2的时候,用的成功或者失败跳转提示页面一般是用框架的.在后续开发过程中,根据实际项目需要,也是可以更改的,在此分享一 ...
- tp3.2读取time()格式遇到的的问题(尚未解决)
在用tp3.2框架做一个讲座模块.最近又遇到了一个问题 如上图所示,我把日期和讲座开始时间结束时间分来放了.(这里的Jdate2和jdate3本来存放为time(7)类型的,后发现在原来这个7是可以改 ...
- java中的访问修饰符 (2013-10-11-163 写的日志迁移
访问级别 修饰符 同类 同包 子类 ...