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, " ...
随机推荐
- NOIP专题复习3 图论-强连通分量
目录 一.知识概述 二.典型例题 1.[HAOI2006]受欢迎的牛 2.校园网络[[USACO]Network of Schools加强版] 三.算法分析 (一)Tarjan算法 (二)解决问题 四 ...
- 笔试算法题(35):最长递增子序列 & 判定一个字符串是否可由另一个字符串旋转得到
出题:求数组中最长递增子序列的长度(递增子序列的元素可以不相连): 分析: 解法1:应用DP之前需要确定当前问题是否具有无后效性,也就是每个状态都是对之前状态的一个总结,之后的状态仅会受到前一个状态的 ...
- 在springBoot的控制台打印sql语句
在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: properties形式 logging.level.com.eth.wallet.mapp ...
- python爬虫框架—Scrapy安装及创建项目
linux版本安装 pip3 install scrapy 安装完成 windows版本安装 pip install wheel 下载twisted,网址:http://www.lfd.uci.edu ...
- 79-Envelopes,包络指标.(2015.7.1)
Envelopes 包络指标 观井映天 2015.7.1
- 对SpringMVC框架的理解(转)
SpringMVC概念: 他是一个轻量级的开源框架,应用于表现层,基于MVC的设计模式. SpringMVC的特点: 1.他是单例的可以设置成多例. 2.他的线程是安全的 ...
- Django——分页功能Paginator
Django分页功能----Paginator Paginator所需参数: Paginator(object_list,per_page) Paginator常用属性: per_page: 每页显示 ...
- 一个ajax实例
一个ajax实例 html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ch ...
- MVC系统学习6—Filter
Mvc的过滤器是特性类,可以使我们在执行Action之前,执行Action之后,执行Action发生异常时,编写相关的处理代码实现某些逻辑.下面是四个基本的Filter接口. 上面这四个基本的Filt ...
- linux & command line & console & logs
linux & command line & console & logs how to get the logs form linux command console htt ...