树形dp____Magic boy Bi Luo with his excited tree( hdu 5834 2016ccpc网络赛)

来源:互联网 发布:七天网络查询分数 编辑:程序博客网 时间:2024/06/05 10:27

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(T104) , represents there are T test cases.

Four each test:

The first line contain an integer N(N105).

The next line contains N integers V[i], which means the treasure’s value of node i(1V[i]104).

For the next N1 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(1c104).

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
154 1 7 7 7 1 2 61 3 12 4 83 5 2
 

Sample Output
Case #1:151014915
 


题意:

一个n个节点的数,每个节点有一个价值为pi的宝藏,点之间的边有权值,每次经过该边会扣掉该边权值对应的价值(价值可以为负数继续前行).问分别从每点开始走,最多可以获得多少价值?


分析:

树形dp。需要讨论的细节比较多。

需要记录两个数据。f[N][2],g[N][2]。以节点1为根节点。

对于f[i][0/1]:f[i][0],表示从i点开始走i - i父亲支路并且回到i点的最大价值,f[i][1] 表示从i开始走i - i父亲支路并且不用回到i点的最大价值。(注意:都不包括i点本身的价值).

对于g[i][0/1]:g[i][0]:表示从i点开始走向子孙节点并且回到i点的最大价值,g[i][1] 表示从i点开始走向子孙节点并且不用回到i点的最大价值。( 注意:都包括i点本身价值).

所以对于任意一点的最大价值为: max( g[i][0]+f[i][1] , g[i][1]+f[i][0] ).

至于f,g的状态转移看代码吧。


代码:

#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=100010;const int mod=1000000007;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int INF=1000000010;const ll MAX=1ll<<55;const double eps=1e-5;const double inf=~0u>>1;const double pi=acos(-1.0);typedef double db;typedef unsigned int uint;typedef unsigned long long ull;int tot,u[N],v[2*N],w[2*N],pre[2*N];void add(int x,int y,int z) {    v[tot]=y;w[tot]=z;pre[tot]=u[x];u[x]=tot++;    v[tot]=x;w[tot]=z;pre[tot]=u[y];u[y]=tot++;}int q[N],fa[N],g1[N],g[N][2],f[N][2];//处理g//x当前,y之前//g[i][0/1] 从i出发向儿子走,0表示回到i,1表示停在某个子孙//g1是当从i出发向儿子走停在某个子孙的情况下收获最大而选择的儿子。void dfs1(int x,int y) {    g[x][0]=g[x][1]=q[x];g1[x]=x;fa[x]=y;    for (int i=u[x];i!=-1;i=pre[i])    if (v[i]!=y) {        dfs1(v[i],x);        //其他路进去,当前路回来。        g[x][1]=max(g[x][1],g[x][1]+g[v[i]][0]-2*w[i]);         if (g[x][0]+g[v[i]][1]-w[i]>g[x][1]) g1[x]=v[i];        //其他路回来,当前路进去。        g[x][1]=max(g[x][1],g[x][0]+g[v[i]][1]-w[i]);        g[x][0]+=max(0,g[v[i]][0]-2*w[i]);    }}//x当前结点,前置结点,z是前置代价//f[i][0/1] 0 i-父亲这条路径走两遍的最大价值(不包括i点价值) 1 i-父亲这两条路径走一遍的最大价值。void dfs2(int x,int y,int z) {    f[x][0]=f[x][1]=0;     //对于y这个点,一定有y-x这个分支,但是因为求得是g[y][0]是需要回到y点,因为当g[x][0] <= 2*z 那么一定不会走这个分支。    if (g[x][0]<=2*z) {        f[x][0]=max(f[x][0],f[y][0]+g[y][0]-2*z);          f[x][1]=max(f[x][1],f[y][1]+g[y][0]-z);    }    else {        //仔细画图算每个路径是否被求就可以证明下面两个等式成立。                f[x][0]=max(f[x][0],f[y][0]+g[y][0]-g[x][0]);        f[x][1]=max(f[x][1],f[y][1]+g[y][0]-g[x][0]+z);    }    //表示y-x这个支路是g[y][1]的支路。    //所以要从其他支路找到次大的利益    if (g1[y]==x) {        int i,mx0=q[y],mx1=q[y];        for (i=u[y];i!=-1;i=pre[i])        if (v[i]!=fa[y]&&v[i]!=x) {            mx1=max(mx1,mx1+g[v[i]][0]-2*w[i]);            mx1=max(mx1,mx0+g[v[i]][1]-w[i]);            mx0+=max(0,g[v[i]][0]-2*w[i]);        }        //mx1 表示从y开始往子孙走不回来的最大利益(不会走y-x分支).        //mx0 表示从y走开始往子孙走并且回来的最大利益(不会走y-x分支).        f[x][1]=max(f[x][1],mx1+f[y][0]-z);    } else {        if (g[x][0]<=2*z) f[x][1]=max(f[x][1],f[y][0]+g[y][1]-z);        else f[x][1]=max(f[x][1],f[y][0]+g[y][1]-g[x][0]+z);    }    for (int i=u[x];i!=-1;i=pre[i])    if (v[i]!=y) dfs2(v[i],x,w[i]);}int main(){    int i,n,t,x,y,z,ca;    scanf("%d", &t);    f[0][0]=f[0][0]=0;    g[0][0]=g[0][1]=0;    for (ca=1;ca<=t;ca++) {        scanf("%d", &n);        tot=0;memset(u,-1,sizeof(u));        for (i=1;i<=n;i++) scanf("%d", &q[i]);        for (i=1;i<n;i++) scanf("%d%d%d", &x, &y, &z),add(x,y,z);        dfs1(1,0);dfs2(1,0,0);        printf("Case #%d:\n", ca);        for (i=1;i<=n;i++) printf("%d\n", max(g[i][0]+f[i][1],g[i][1]+f[i][0]));    }    return 0;}





0 0
原创粉丝点击