分析:典型的两遍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的更多相关文章

  1. 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: ...

  2. 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 ...

  3. HDU5834 Magic boy Bi Luo with his excited tree (树形DP)

    题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...

  4. 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 ...

  5. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  6. 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 ...

  7. 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  8. 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...

  9. HDU 5834 Magic boy Bi Luo with his excited tree

    树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...

随机推荐

  1. mac 解决mysql启动不了

    mac升级了mysql,没有替换原来的链接的情况下, 启动mysql restart 会失败 解决方法,找到mysql真正的安装目录 mysql.server start

  2. keil mdk uvision使用技巧

    语法检测&代码提示 中文友好: tab 可以选中一大块代码,一起缩进 快速注释 先选中你要注释的代码区,然后右键,选择Advanced,Comment Selection 就可以了 查找替换 ...

  3. 用bootstrap_table实现html 表格翻页

    资料网址 百度经验:HTML表格分页,table分页怎么做? 官网(下载链接和官方教程) (右上角可选语言) 文档 以下内容基本摘自官网 用法 1.下载资料 官网下载: 下下来长这样: 其中src里面 ...

  4. TestNG 练习

    java文件 package selniumhomework; import org.testng.annotations.Test; public class Test1 { @Test(group ...

  5. hihoCoder#1042 跑马圈地

    原题地址 经网友jokeren提醒,后面给出的代码虽然可以AC原题,但存在bug,主要是在矩形覆盖情况的判断上处理的不够完全. 看似挺复杂的,但是仔细分析一下可以化简: 首先,不用枚举周长,因为更长的 ...

  6. 【BZOJ3925】地震后的幻想乡(期望概率DP,状压DP)

    题意:给定一张点数不超过10的无向连通图,每条边有一个[0,1]之间的随机权值,求最小生成树上最大边的期望值 提示:对于n个[0,1]之间的随机变量x1,x2,...,xn,第k小的那个的期望值是k/ ...

  7. Linux下汇编语言学习笔记22 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  8. 安装最新版本的zabbix

    1. 先安装php5.4 最新版本: yum安装php5.4或5.5 https://blog.csdn.net/MarkBoo/article/details/49424183 2. 然后参照官网或 ...

  9. 51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先 ...

  10. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...