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: 131072/131072 K (Java/Others)
Total Submission(s): 1058 Accepted Submission(s): 308
You may attention that every V[i] can be taken only once, but for some C[i] , you may cost severial times.
Now, Bi Luo define ans[i] as the most value can Bi Luo gets if Bi Luo starts at node i.
Bi Luo is also an excited boy, now he wants to know every ans[i], can you help him?
Four each test:
The first line contain an integer N(N≤105).
The next line contains N integers V[i], which means the treasure’s value of node i(1≤V[i]≤104).
For the next N−1 lines, each contains three integers u,v,c , which means node u and node v are connected by an edge, it's cost is c(1≤c≤104).
You can assume that the sum of N will not exceed 106.
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2
15
10
14
9
15
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int val[N],dp[N][6],ans[N];
struct edge{
int v,c;
};
vector<edge> G[N]; void dfs1(int u,int f)
{
dp[u][1]=dp[u][0]=val[u];
for(int i=0;i<G[u].size();i++){
int v=G[u][i].v,c=G[u][i].c;
if(v==f) CT;
dfs1(v,u);
int cur=dp[u][0]-c+dp[v][1],
ano1=dp[u][1]+max(dp[v][0]-2*c,0),
ano2=dp[u][2]+max(dp[v][0]-2*c,0);
if(cur>ano1){
dp[u][4]=v;
dp[u][1]=cur;
dp[u][2]=ano1;
}
else if(cur>ano2) {
dp[u][1]=ano1;
dp[u][2]=cur;
}
else{
dp[u][1]=ano1;
dp[u][2]=ano2;
}
dp[u][0]+=max(0,dp[v][0]-2*c);
}
} void dfs2(int u,int f,int fback,int fnback)
{
ans[u]=max(dp[u][0]+fnback,dp[u][1]+fback);
for(int i=0;i<G[u].size();i++){
int v=G[u][i].v,c=G[u][i].c;
if(v==f) CT;
int uback=fback+dp[u][0]-max(0,dp[v][0]-2*c)-2*c,unback;
if(v==dp[u][4]){
unback=max(dp[u][0]-max(0,dp[v][0]-2*c)+fnback,
fback+dp[u][2]-max(0,dp[v][0]-2*c))-c;
}
else{
unback=max(fnback+dp[u][0]-max(0,dp[v][0]-2*c),
fback+dp[u][1]-max(0,dp[v][0]-2*c))-c;
}
dfs2(v,u,max(0,uback),max(0,unback));
}
} int main()
{
int cas,kk=0;
SC("%d",&cas);
while(cas--){
int n;SC("%d",&n);
MM(dp,0);
for(int i=1;i<=n;i++){
SC("%d",&val[i]);
G[i].clear();
}
for(int i=1;i<=n-1;i++){
int u,v,c;
SC("%d%d%d",&u,&v,&c);
G[u].push_back((edge){v,c});
G[v].push_back((edge){u,c});
}
dfs1(1,-1);
dfs2(1,-1,0,0);
printf("Case #%d:\n",++kk);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}
return 0;
}
hdu 5834 Magic 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)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- 动态规划(树形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. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
- HDU5834 Magic boy Bi Luo with his excited tree (树形DP)
题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...
- HDU5834Magic boy Bi Luo with his excited tree 树形dp
分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从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个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
随机推荐
- centos7 宝塔php7安装mongodb扩展
一.下载.解压源码 下载地址:https://pecl.php.net/package/mongodb wget -c https://pecl.php.net/get/mongodb-1.5.3.t ...
- golang数据基本数据类型和string类型的转换
基本类型之间的转换 golang在不同类型的变量之间赋值时需要显式转换,也就是说golang中数据类型不能自动转换. 表达式T(v)将值v转换为类型T 1.数据类型的转换可以是从范围小——>范围 ...
- ztree入门
ztree入门 ztree可用于权限管理,机构部门等有层次的数据 准备工作 ztree官网 点击右上角的GitHub或者码云的地址将demo下载到本地 在本地新建一个项目,将下载的文件中zTreeSt ...
- Pattern Recognition and Machine Learning-01-Preface
Preface Pattern recognition has its origins in engineering, whereas machine learning grew out of com ...
- 客户端相关知识学习(十)之app给h5传递数据
方法一: app可以把参数传到h5的链接里,用类似?xx=xx&xx=xx的形式拼接,js解析参数即可. 方法二: 情况一:app调用h5 原生app都可以对js的function进行触发,前 ...
- [NOIP10.6模拟赛]1.merchant题解--思维+二分
题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...
- Ubuntu/centos/redhat/SUSE sipp安装(带rtp支持,3.5.1版本)
1.ubuntu 12.04 apt-get install ncurses-dev apt-get install libpcap-dev ./configure --with-pcap make ...
- JS笛卡尔积算法与多重数组笛卡尔积实现方法示例
js 笛卡尔积算法的实现代码,据对象或者数组生成笛卡尔积,并介绍了一个javascript多重数组笛卡尔积的例子,以及java实现笛卡尔积的算法与实例代码. 一.javascript笛卡尔积算法代码 ...
- Java获取近7个月的起止时间
话不多说,直接上代码 public class Test { @org.junit.Test public void tets() { SimpleDateFormat format = new Si ...
- Caffe之layer_factory
之前在测试NN中各个层的时间的时候,遇到一个非常奇怪的问题,分别使用Caffe自己的gpu方法和cuDNN方法,在卷积上性能差异非常大,但是在pooling层上基本没有变化.抽空检查了代码之后,发现是 ...