树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以走多次,问从每个点出发的最大获益
// 思路:
// dp[i]: 从i点出发回到i点的最大值
// d[i][0] 从i点出发不回来的最大值
// d[i][1] 从i点出发取最大值的下一个点
// d[i][2] 从i点出发取次大值
// dfs1处理出这四个
// 然后,从1开始转移,分别DP求子节点从父亲转移过来的最大值,和从父亲出去不回来的最大值 #include <bits/stdc++.h>
using namespace std;
#define LL long long
const double inf = 123456789012345.0;
const LL MOD =100000000LL;
const int N =1e5+;;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} vector<pair<int,int> > g[N];
int a[N],ans[N];
int dp[N],d[N][];
void dfs1(int u,int f){
dp[u]=a[u];
for(int i=;i<(int)g[u].size();i++){
int v=g[u][i].first;
int val=g[u][i].second;
if(v==f) continue;
dfs1(v,u);
dp[u]+=max(,dp[v]-*val);
}
d[u][]=d[u][]=a[u];
d[u][]=-;
for(int i=;i<(int)g[u].size();i++){
int v=g[u][i].first;
int val=g[u][i].second;
if(v==f) continue;
int tem=dp[u]-max(,dp[v]-*val)+max(,d[v][]-val);//当前选择路径不回来的最大值
if(tem>d[u][]){
d[u][]=d[u][];
d[u][]=tem;
d[u][]=i;
}
else if(tem>d[u][]){
d[u][]=tem;
}
}
} //no从父亲不回来的最大值,yes从父亲回来的最大值
void dfs2(int u,int f,int no,int yes){
ans[u]=max(dp[u]+no,yes+d[u][]);
for(int i=;i<(int)g[u].size();i++){
int v=g[u][i].first;
int val=g[u][i].second;
if(v==f) continue;
int tem1,tem2;
if(i==d[u][]) tem1=max(,d[u][]-max(,dp[v]-*val));//从父亲出去但不是最优方案的最大值
else tem1=max(,d[u][]-max(,dp[v]-*val));
tem2=max(,dp[u]-max(,dp[v]-*val));//从父亲出去又回来的最大值
tem1=max(,max(tem1+yes-val,tem2+no-val));//儿子节点从父亲转移过来的是 max(父亲儿子节点出去不回来+父亲的父亲出去回来的最大值,父亲儿子节点出去回来+父亲的父亲出去不回来的最大值)
tem2=max(,yes+tem2-*val);
dfs2(v,u,tem1,tem2);
}
} int main(){
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++){
int u,v,x;
scanf("%d%d%d",&u,&v,&x);
g[u].push_back(make_pair(v,x));
g[v].push_back(make_pair(u,x));
}
dfs1(,-);
dfs2(,-,,);
printf("Case #%d:\n",cas);
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}
return ;
}
树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree的更多相关文章
- 动态规划(树形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(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- 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 ...
- 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 ...
- hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】
题目链接 hdu5834 题解 思路很粗犷,实现很难受 设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益 设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益 再设\(h[i ...
- HDU5834 Magic boy Bi Luo with his excited tree (树形DP)
题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...
- 【树形动规】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+转移
Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 13107 ...
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
随机推荐
- Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、<meta>
1. package mypack; import java.lang.reflect.Constructor; import org.hibernate.*; import org.hibernat ...
- Android:Context的作用
Context字面意思上下文,Activity中我们直接用this代替,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,改用ActivityName.t ...
- Tomcat启动后访问首页报错 显示JSP 空指针异常
HTTP Status 500 - type Exception report message description The server encountered an internal error ...
- UIColor的用法
UIColor,CGColor,CIColor的区别和联系 layer.shadowColor = [UIColor redColor].CGColor; 这个是今天用到的.顺便总结一下. 1.UIC ...
- AWS Python SDK boto3中的基本概念与使用方法
最近在用boto3编写AWS的lamda函数,学习到了boto3中的一些基本概念与使用方法.在此进行总结. 1. boto3提供了两个级别的接口来访问AWS服务:High Level的Resource ...
- Android 按键消息处理Android 按键消息处理
在android系统中,键盘按键事件是由SystemServer服务来管理的:然后在以消息的形式分发给应用程序处理.产生键盘按键事件则是有Linux kernel的相关驱动来实现. 键盘消息有别于其他 ...
- Android 获取屏幕尺寸与密度
android中获取屏幕的长于宽,参考了网上有很多代码,但结果与实际不符,如我的手机是i9000,屏幕大小是480*800px,得到的结果却为320*533 结果很不靠谱,于是自己写了几行代码,亲 ...
- Qt之启动外部程序
简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...
- SelectSingleNode和SelectNodes区别
SelectSingleNode:选择匹配 XPath 表达式的第一个 XmlNodeSelectNodes:选择匹配 XPath 表达式的结点集合 XmlNodeList
- [POJ 3498] March of the Penguins
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4378 Accepted: ...