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

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

2016中国大学生程序设计竞赛 - 网络选拔赛

题意:给你一棵树,每个结点有个宝藏价值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;
}

 题解:

时间: 2024-08-27 01:09:09

hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移的相关文章

HDU 5834 Magic boy Bi Luo with his excited tree (树形DP)

题意:给定一棵树,每个点有个权值,每条边有权值,每经过边都会消耗相应的权值,但是点的权值只能获得一次,问你从 i 点出发能获得的最大权值是多少. 析:树形DP,就是太麻烦了,两次dfs,维护一共6个值分别是,从 i 出发的最大值并且返回 i, 从 i 出发的最大值并且不返回,从 i 出发的次大值并且不返回,从 i 出发的最大值的子树结点并且不返回,从 i 向父结点出发的最大值并且不返回,从 i 向父结点出发的最大值并且返回. 第一次dfs就能求出前四个,第二个dfs维护后面两个. 答案就是  m

HDU 5834 Magic boy Bi Luo with his excited tree

题目:Magic boy Bi Luo with his excited tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意:给一棵树,在树的每个结点可以收获一定的积分,每一条边会消耗一定的积分,每个结点积分取一次便完,但每次路过一条边,都会消耗积分,问从每一个结点出发,最多能捞多少积分.N范围10万,样例数1万. 思路: 不难的一道题,但细节较多,容易出错. 树形DP. 从i 点出发,大致可以分为向上 和 向下两种,先考虑向下的情

Magic boy Bi Luo with his excited tree (树形dp)

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

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

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边权每次经过都要扣,问从每一个节点开始走最大能获得的价值. 题目思路: [树形动态规划] 首先用dfs求出从根1往下走的:节点u往下走最后回到节点u的最大值g[u],节点u往下走最后不回到u的最优值和次优值f[0][u],f[1][u] 接着考虑一个节点u,除了以上的情况还有可能是往它的父亲方向走,这

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

非常难想的树形DP. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int N=100010; 6 int cnt,fir[N],nxt[N*2],to[N*2],val[N*2]; 7 int n,w[N],ans[N],fa[N],f1[N][2],f2[N][2]; 8 void addedge(int a,int b,in

&lt;hdu5834 Magic boy Bi Luo with his excited tree&gt; (树形DP)

题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i节点的儿子中走又回到i的最大值 dp[i][1]表示不回到i的最大值 dp[i][2]表示不回到i的次大值 同时需要记录不回到i最大值的方向id[x] 很显然 第一遍dfs可以预处理每个节点往下的值 然后关键的就是每个节点从父亲这个方向的值怎么处理 有个很显然的结论就是 不回来是肯定比回来更优的 所

【HDOJ 5834】Magic boy Bi Luo with his excited tree(树型DP)

[HDOJ 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) Problem Description Bi Luo is a magic boy, he also has a migic tree,

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

1 // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree 2 // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以走多次,问从每个点出发的最大获益 3 // 思路: 4 // dp[i]: 从i点出发回到i点的最大值 5 // d[i][0] 从i点出发不回来的最大值 6 // d[i][1] 从i点出发取最大值的下一个点 7 // d[i][2] 从i点出发取次大值 8 // dfs1处理出这四个 9

hdu5834 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): 723    Accepted Submission(s): 192 Problem Description Bi Luo is a magic boy, he also has a migic tree,