hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】
题目链接
题解
思路很粗犷,实现很难受
设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益
设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益
再设\(h[i]\)为\(f[i][0]\)的次优收益
对于\(f[i][1]\),贪心选择所有\(f[v][1] - 2 * w \ge 0\)的子树即可
对于\(f[i][0]\),贪心选择所有没有被选的子树的\(f[v][0] - w \le 0\)的最大值 或者 被选子树\(f[v][1] - 2 * w\)改成\(f[v][0] - w\)后多产生收益的最大值
同时维护次优\(h[v]\)
对于\(g[i][1]\),设父亲为\(v\),就等于\(f[v][1] + g[v][1]\)再减去\(i\)对\(f[v][1]\)所作出的贡献【因为往父亲走要忽视\(i\)这课子树】
对于\(g[i][0]\)也是类似的,但是由于忽视\(i\)这课子树后\(f[i][0]\)的决策可能发生改变,所以要在之前算好次优决策\(h[v]\)
这种树形dp简单题都做不出了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = head[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 = 100005,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 head[maxn],ne = 2;
struct EDGE{int to,nxt,w;}ed[maxn << 1];
inline void build(int u,int v,int w){
ed[ne] = (EDGE){v,head[u],w}; head[u] = ne++;
ed[ne] = (EDGE){u,head[v],w}; head[v] = ne++;
}
int n,fa[maxn],d[maxn],w[maxn],f[maxn][2],g[maxn][2],h[maxn],way[maxn];
//cal son
void dfs1(int u){
f[u][0] = f[u][1] = w[u];
int mx = -INF,v,tmp,mx2 = -INF;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; d[to] = ed[k].w; dfs1(to);
if (f[to][1] - 2 * d[to] >= 0){
f[u][1] += f[to][1] - 2 * d[to];
tmp = (f[to][0] - d[to]) - (f[to][1] - 2 * d[to]);
if (tmp > mx) mx2 = mx,mx = tmp,v = to;
else if (tmp > mx2) mx2 = tmp;
}
else if ((tmp = f[to][0] - d[to]) >= 0){
if (tmp > mx) mx2 = mx,mx = tmp,v = to;
else if (tmp > mx2) mx2 = tmp;
}
}
if (mx >= 0) f[u][0] = f[u][1] + mx,way[u] = v;
else f[u][0] = f[u][1],way[u] = 0;
if (mx2 >= 0) h[u] = f[u][1] + mx2;
else h[u] = f[u][1];
}
//cal father
void dfs2(int u){
int v = fa[u];
//back
if (f[u][1] - 2 * d[u] >= 0)
g[u][1] = max(0,f[v][1] + g[v][1] - (f[u][1] - 2 * d[u]) - 2 * d[u]);
else g[u][1] = max(0,f[v][1] + g[v][1] - 2 * d[u]);
//not back
if (f[u][1] - 2 * d[u] >= 0){
g[u][0] = max(0,f[v][1] + g[v][0] - (f[u][1] - 2 * d[u]) - d[u]);
if (way[v] == u)
g[u][0] = max(g[u][0],h[v] + g[v][1] - (f[u][1] - 2 * d[u]) - d[u]);
else g[u][0] = max(g[u][0],f[v][0] + g[v][1] - (f[u][1] - 2 * d[u]) - d[u]);
}
else{
g[u][0] = max(0,f[v][1] + g[v][0] - d[u]);
if (way[v] == u)
g[u][0] = max(g[u][0],h[v] + g[v][1] - d[u]);
else g[u][0] = max(g[u][0],f[v][0] + g[v][1] - d[u]);
}
Redge(u) if ((to = ed[k].to) != fa[u])
dfs2(to);
}
int main(){
int T = read();
REP(C,T){
n = read(); ne = 2;
REP(i,n) w[i] = read(),head[i] = 0;
int a,b,w;
for (int i = 1; i < n; i++){
a = read(); b = read(); w = read();
build(a,b,w);
}
dfs1(1);
dfs2(1);
printf("Case #%d:\n",C);
REP(i,n) printf("%d\n",max(f[i][1] + g[i][0],f[i][0] + g[i][1]));
}
return 0;
}
hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】的更多相关文章
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- HDU5834 Magic boy Bi Luo with his excited tree (树形DP)
题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...
- hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移
Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 13107 ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- HDU5834 Magic boy Bi Luo with his excited tree(树形DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...
- HDU5834Magic boy Bi Luo with his excited tree 树形dp
分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来 吐槽:赛场上想到了状态,但是不 ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
随机推荐
- Java环境变量搭建(Linux环境)
1. 下载解压JDK压缩包 例如:解压到 /opt/jdk1.7.0_80 下 2. 添加环境变量到 /etc/profile 文件中 vi /etc/profile 在文件末尾追加如下内容: exp ...
- css属性选择器=,~=,^=,$=,*=,|=
http://www.w3school.com.cn/css/css_selector_attribute.asp =. property和value必须完全一致 : ~=.“约等于”?: ^=. 从 ...
- jquery淡入淡出轮播图
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- dijkstra算法优先队列
d[i] 是起点到 I 节点的最短距离 void Dijkstra(int s) { priority_queue<P, vector<P>, greater<P> &g ...
- 文档处理jQuery,实现添加删除复制
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 配置vim nginx.conf高亮
#!/bin/bashwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vimmv nginx.vim ...
- nodejs源码—初始化
概述 相信很多的人,每天在终端不止一遍的执行着node这条命令,对于很多人来说,它就像一个黑盒,并不知道背后到底发生了什么,本文将会为大家揭开这个神秘的面纱,由于本人水平有限,所以只是讲一个大概其,主 ...
- python3 包的发布
发布流程大概如下 1. 首先需要有一个python包,就是一个文件夹,但是此文件夹下面有__init__.py文件,里面内容是 现在要发布包TestMsg,这就是一个python包.在同级目录下新建s ...
- Python中的dict
dict_lst = [ ('字典的键必须可哈希',), ('字典的键重复覆盖',), ('字典可迭代') ('增',), ('删',), ('改',), ('查',), ('练习',), ] 字典的 ...
- 全球征集-如何实现回文SQL的查询
有个表,以下是创建的SQL: CREATE TABLE [dbo].[SysName]( ,) NOT NULL, ) COLLATE Chinese_PRC_CI_AS NULL, ) COLLAT ...