分析:典型的两遍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. mysql8忘记登录密码时,修改密码方法

    一.停止mysqld进程 systemctl stop mysqld.service 二.修改/etc/my.cnf(在mysqld选项添加skip-grant-tables),设置免密码登录: vi ...

  2. pip各种

    pip: 一个现代的,通用的 Python 包管理工具.提供了对Python 包的查找.下载.安装.卸载的功能. windows:自带pip,直接使用. Linux:执行下面命令即可完成安装. # w ...

  3. LeetCode(46)Permutations

    题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...

  4. LeetCode(88)Merge Sorted Array

    题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...

  5. noip模拟赛 终末

    分析:举个例子就能发现:偶数位上的数都必须是0,奇数位上的数可以取0~k-1,这就是一个标准的数位dp了. 这编译器......数组越界了竟然不报错. #include <cstdio> ...

  6. 天才的记忆(vijos 1514)

    描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以继承他的宝藏.题目 ...

  7. Meeting 加虚拟边

    Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his fence ...

  8. UVA 10564_ Paths through the Hourglass

    题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...

  9. bzoj1444 有趣的游戏(AC自动机+概率dp)

    题意: 给定n个长度为l的模式串,现在要用前m个大写字母生成一个随机串,每个字符有自己的出现几率,第一次出现的字符串获胜,求最终每个字符串的获胜几率. 分析: 容易想到先把所有的字符串建成一个AC自动 ...

  10. eclipse断点有个斜杠 skip all breakpoints

    切换到debug,单击下 skip all breakpoints 即可