HDU5834Magic boy Bi Luo with his excited tree 树形dp
分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来
吐槽:赛场上想到了状态,但是不会更新,而且据说这是一种典型的树形dp,还是太弱
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int BufferSize=<<;
char buffer[BufferSize],*hea,*tail;
inline char Getchar() {
if(hea==tail) {
int l=fread(buffer,,BufferSize,stdin);
tail=(hea=buffer)+l;
}
return *hea++;
}
inline int read() {
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
int T,n,a[N],head[N],tot,ret[N],dp[N][],id[N],kase;
struct Edge{
int v,w,next;
}edge[N<<];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int f){
dp[u][]=dp[u][]=a[u];id[u]=-;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;if(v==f)continue;dfs(v,u);
int tmp=max(,dp[v][]-*edge[i].w);
dp[u][]+=tmp;
if(dp[v][]-edge[i].w>){
if(dp[u][]+dp[v][]-edge[i].w>dp[u][]){
dp[u][]=dp[u][]+dp[v][]-edge[i].w;
id[u]=v;
}
}
dp[u][]+=tmp;
}
}
void get(int u,int f,int x0,int x1){
ret[u]=max(dp[u][]+x1,dp[u][]+x0);
int w1=dp[u][]+x0,w0=dp[u][];
if(w0+x1>=w1){
w1=w0+x1;
id[u]=f;
}
w0+=x0;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;if(v==f)continue;
int tmp,tmp0,tmp1;
if(v!=id[u]){
tmp=max(dp[v][]-*edge[i].w,);
tmp0=w0-tmp,tmp1=w1-tmp;
get(v,u,max(,tmp0-*edge[i].w),max(,tmp1-edge[i].w));
continue;
}
tmp0=a[u]+x0,tmp1=a[u]+x1;
for(int j=head[u];~j;j=edge[j].next){
int to=edge[j].v;if(to==f||to==v)continue;
tmp=max(,dp[to][]-*edge[j].w);
tmp1+=tmp;
if(dp[to][]-edge[j].w>){
if(tmp0+dp[to][]-edge[j].w>tmp1)tmp1=tmp0+dp[to][]-edge[j].w;
}
tmp0+=tmp;
}
get(v,u,max(,tmp0-*edge[i].w),max(,tmp1-edge[i].w));
}
}
int main(){
T=read();
while(T--){
n=read();
for(int i=;i<=n;++i){
head[i]=-;a[i]=read();
}
tot=;
for(int i=;i<n;++i){
int u=read(),v=read(),w=read();
add(u,v,w);add(v,u,w);
}
dfs(,-);
get(,-,,);
printf("Case #%d:\n",++kase);
for(int i=;i<=n;++i)
printf("%d\n",ret[i]);
}
return ;
}
HDU5834Magic 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: ...
- 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 ...
- HDU5834 Magic boy Bi Luo with his excited tree (树形DP)
题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从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 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 ...
- 动态规划(树形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,节点的值只能取一次,边 ...
- HDU 5834 Magic boy Bi Luo with his excited tree
树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
随机推荐
- NodeJs中数据库的使用
另一遍通用的NODEJS数据库方法koa,express,node 通用方法连接MySQL 1.Node.js 连接 MySQL $ cnpm install mysql 连接mysql: var m ...
- <struct、union、enum>差异
关于C++和C的区别 区别最大的是struct,C++中的struct几乎和class一样了,可以有成员函数,而C中的struct只能包含成员变量. enum,union没区别. struct的定义 ...
- use-gulp
参考: https://github.com/Platform-CUF/use-gulp use-gulp 为什么使用gulp? 首先看一篇文章 Gulp的目标是取代Grunt 根据gulp的文档,它 ...
- windows资源监控常用计数器解释
随笔有些是自己写的,有些是根据网上的东西自己整理的,文章基本都是别人的,只是为方便查看复制到那里
- 7-10 公路村村通(30 分)(最小生成树Prim算法)
7-10 公路村村通(30 分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1 ...
- [NOIP2006] 提高组 洛谷P1066 2^k进制数
题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后 ...
- codevs3164 质因数分解
题目描述 Description (多数据)给出t个数,求出它的质因子个数. 数据没坑,难度降低. 输入描述 Input Description 第一行 t 之后t行 数据 输出描述 Output D ...
- 洛谷 P1555 尴尬的数字
P1555 尴尬的数字 题目背景 Bessie刚刚学会了不同进制数之间的转换,但是她总是犯错误,因为她的两个前蹄不能轻松的握住钢笔. 题目描述 每当Bessie将一个数转换成新的进制时,她总会写错一位 ...
- Ubuntu 16.04重启输入法
一般使用搜狗输入法,但是偶尔不太稳定会挂掉,但是可以通过以下脚本重启: pidof fcitx | xargs kill pidof sogou-qimpanel | xargs kill nohup ...
- wget下载网络图片
wget http....... --no-check-certificate