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, " ...
随机推荐
- 动态 SQL(1)
使用动态 SQL 完成多条件查询 动态 SQL 是 MyBatis 的一个强大的特性.在使用 JDBC 操作数据时,如果查询条件特别多,将条件串联成 SQL 字符串是一件痛苦的事情,通常的解决方法是写 ...
- 树莓派 - platform总线,设备和驱动
以树莓派为例子,分析一下其中LED的 platform device 和 platform driver. 查看LED设备,被挂载在/sys/devices/platform下. 注意其中的drive ...
- mysql错误Error(1133): Can’t find any matching row in the use
执行插入用户语句没有问题,但是执行权限赋值的时候提示:1133 - Can't find any matching row in the user table; 解决办法:插入新的用户成功时,需要刷新 ...
- Volume 1. String(uva)
10361 - Automatic Poetry #include <iostream> #include <string> #include <cstdio> # ...
- Win2008 Server搭建流媒体服务(在线看电影)
什么是流媒体服务呢. 所谓流媒体是指采用流式传输的方式在Internet播放的媒体格式, 与需要将整个视频文件全部下载之后才能观看的传统方式相比, 流媒体技术是通过将视频文件经过特殊的压缩方式分成一个 ...
- [学习资料] Tiny210(S5PV210) u-boot移植
Tiny210(S5PV210) u-boot移植http://www.microoh.com/bbs/forum.php?mod=viewthread&tid=254&fromuid ...
- 空循环导致CPU使用率很高
业务背景 业务背景就是需要将多张业务表中的数据增量同步到一张大宽表中,后台系统基于这张大宽表开展业务,所以就开发了一个数据同步工具,由中间件采集binlog消息到kafka里,然后我去消费,实现增量同 ...
- python 调用 C 动态库
首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...
- LeetCode(29)Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [luoguP1058] 立体图(超级大模拟(¬︿̫̿¬☆))
传送门 看到题后整个人成了mengbier 但是仔细分析一下就很简单了,先确定好输出的图的长和宽. 然后从输入的矩形的左上角的最下面的开始填充,顺序是从下到上,从左到右,从后往前. 填充的时候直接覆盖 ...