HDU5834解题报告

树形 dp 套路题,但是转移有点麻烦。一个很显然的想法就是 先 dfs 一次把子树搞完,然后再 dfs 加上其父节点对当前节点的影响。写的略辣眼睛,调试了好久。。

dp[0][u],dp[1][u] 分别表示以u为根,走回来和不走回来的最大值,第一次 dfs 很简单,普通的套路,第二次的时候,我们先把父亲节点对于即将访问的儿子节点的影响排除掉,再 dfs ,然后回溯的时候还原即可,注意 dp[1][u] 的时候要记录下次大,转移的时候,若即将访问的儿子节点 v u 往下走不回来的路上,就用次大值来减去影响。

//
//  Created by Running Photon
//  Copyright (c) 2015 Running Photon. All rights reserved.
//
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 10;
const int maxv = 1e5 + 10;
const double eps = 1e-9;

int pnt[maxn], nxt[maxn], val[maxv], head[maxv], cost[maxn], cnt;
void add_edge(int u, int v, int value) {
    pnt[cnt] = v;
    cost[cnt] = value;
    nxt[cnt] = head[u];
    head[u] = cnt++;
}
int dp[2][maxv];
void dfs1(int u, int fa) {
    dp[0][u] = val[u];
    dp[1][u] = val[u];
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = pnt[i];
        if(v == fa) continue;
        dfs1(v, u);
        if(dp[0][v] - cost[i] * 2 > 0) {
            dp[0][u] += dp[0][v] - cost[i] * 2;
        }
    }
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = pnt[i];
        if(v == fa) continue;
        if(dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]) >= dp[1][u]) {
            dp[1][u] = dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]);
        }
    }
}
void dfs2(int u, int fa, int Cost) {
    int dir = u, tmp;
    if(fa != -1 && dp[0][fa] - Cost * 2 > 0) {
        dp[0][u] += dp[0][fa] - Cost * 2;
    }
    tmp = dp[1][u] = dp[0][u];
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = pnt[i];

        if(dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]) >= dp[1][u]) {
            dir = v;
            swap(tmp, dp[1][u]);
            dp[1][u] = dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]);
        }
        else if(dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]) >= tmp) {
            tmp = dp[0][u] - max(0, dp[0][v] - cost[i] * 2) + (dp[1][v] - cost[i]);
        }
    }
    // printf("dp[0][%d] = %d dp[1][%d] = %d  tmp = %d\n", u, dp[0][u], u, dp[1][u], tmp);
    int L = dp[0][u], R = dp[1][u];
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = pnt[i];
        if(v == fa) continue;
        if(dp[0][v] - cost[i] * 2 > 0) {
            dp[0][u] = L - (dp[0][v] - cost[i] * 2);
        }
        if(dir == v) {
            dp[1][u] = tmp;
            if(dp[0][v] - cost[i] * 2 > 0) dp[1][u] -= (dp[0][v] - cost[i] * 2);
        } else if(dp[0][v] - cost[i] * 2 > 0) {
            dp[1][u] = R - (dp[0][v] - cost[i] * 2);
        }
        // printf("dir = %d\n", dir);
        // printf("nxt%d = %d\n", v, dp[1][u]);
        dfs2(v, u, cost[i]);
        dp[0][u] = L, dp[1][u] = R;
    }
}
int main() {
#ifdef LOCAL
    freopen("C:\\Users\\Administrator\\Desktop\\in.txt", "r", stdin);
    freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
#endif
//  ios_base::sync_with_stdio(0);
    int T;
    int cas = 0;
    scanf("%d", &T);
    while(T--) {
        int n;
        printf("Case #%d:\n", ++cas);
        scanf("%d", &n);
        cnt = 0;
        memset(head, -1, sizeof (int) * (n + 1));
        for(int i = 1; i <= n; i++) scanf("%d", val + i);
        for(int i = 1; i < n; i++) {
            int u, v, value;
            scanf("%d%d%d", &u, &v, &value);
            add_edge(u, v, value);
            add_edge(v, u, value);
        }
        dfs1(1, -1);
        dfs2(1, -1, 0);
        for(int i = 1; i <= n; i++) {
            printf("%d\n", max(dp[0][i], dp[1][i]));
        }
    }

    return 0;
}

你可能感兴趣的:(动态规划)