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

Problem Description
Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means every time you pass the edge i , you need to pay C[i].

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?

 
Input
First line is a positive integer T(T≤104) , represents there are T test cases.

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.

 
Output
For the i-th test case , first output Case #i: in a single line , then output N lines , for the i-th line , output ans[i] in a single line.
 
Sample Input
1
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2
 
Sample Output
Case #1:
15
10
14
9
15
 
Author
UESTC
 
Source
题意:给你一棵树,每个结点有个宝藏价值w,每次只能拿一次宝藏,每次经过路径需要花费val值,路径可以来回经过,同时可以多次花费val值,求从i点出发,能拿到的最大权值ans【i】
#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+转移的更多相关文章

  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)

    http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...

  3. 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...

  4. 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  5. HDU 5834 Magic boy Bi Luo with his excited tree

    树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...

  6. HDU5834 Magic boy Bi Luo with his excited tree (树形DP)

    题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...

  7. HDU5834Magic boy Bi Luo with his excited tree 树形dp

    分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来 吐槽:赛场上想到了状态,但是不 ...

  8. 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 ...

  9. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

随机推荐

  1. Snoopy.class.php介绍

    Snoopy是一个开源的模拟抓取工具,找到一个不错的介绍网页 记录一下: php开源采集类Snoopy.class.php功能使用介绍与下载地址 Snoopy.class.php使用手册 还有一个介绍 ...

  2. Scrapy爬虫-win7下创建运行项目

    开始的时候,我只安装了python3.5,安装不了scrapy库,网上搜了一下说是scrapy不支持python3.x 然后,我就又安装了python2.7 为了,默认使用2.7,我在环境变量path ...

  3. centos7 yum安装nginx和 编译安装tengine

    说明 我这里给大家演示一下如何安装nginx,nginx我就不多介绍了,然后我再说一点就是,安装的两种方法都可以,编译安装和yum安装,我不能每个都演示两遍呀,所以看到我这博客的你,学会举一反三好吧? ...

  4. PB事件/函数的触发机制和触发方式

    PB作为windows下的一个非常便捷的DB开发工具,有着和windows一样的消息触发机制PB提供了相应event/function触发机制和触发方式,用户可以根据自己的实际需要选用不同方法. 1. ...

  5. grpc的demo

    一 grpc的为什么比http快? 1- 用Proto Buffer 作为序列化工具 2- 采用http2协议,头部压缩,多路复用 3- 基于netty的IO框架 二 grpc的demo A lib工 ...

  6. Css解决表格超出部分用省略号显示

    小伙伴们有没有的遇到页面显示时,因为数据太长导致显示的表格某一列过长,从而导致页面的不美观,下面我们来看一看如何用Css样式解决表格超出部分用省略号显示的问题. 主要设置两个样式: table{ ta ...

  7. tinymce富文本是在modal框中弹出显示的问题

    记录一下,在用tinymce富文本的时候,由于是用在modal 上的,始终无法获取焦点,后来才发现问题出在tinymce在modal前创建了,所以导致这个问题,解决方案就是用 v-if="v ...

  8. Swagger学习(二、配置swagger)

    基于上一篇 其实只是在SwaggerConfig.class中配置docket,apiInfo @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 ...

  9. Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26

    Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/TH ...

  10. idea内存溢出解决方法

    在Run/Debug configuration 的vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=256m eclipse: -Xm ...