HDU 4616 Game 树形DP

Problem Description
  Nowadays, there are more and more challenge game on TV such as ‘Girls, Rush Ahead’. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.

Input
  The first line contains an integer T, indicating the number of testcases.
  For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected.
  All gifts’ value are bigger than 0.

Output
  For each testcase, output the maximum total value of gifts you can get.

Sample Input
2 3 1 23 0 12 0 123 1 0 2 2 1 3 2 23 0 12 0 123 1 0 2 2 1

Sample Output
146 158

Author
SYSU

Source
2013 Multi-University Training Contest 2

给定一棵树,每一个节点有一个权值,有的节点可能存在陷阱,经过一个顶点可以能获得权值但也经历陷阱。找出一条路径,起点自选,
使在经历陷阱数不超过C的情况下,获得的最大的权值是多少?
解题思路:一道很好的树形dp。如果把陷阱去掉,应该算一个经典的题目。其实加上一样:dp[u][j][k]表示到节点u经过j个陷阱,并要往根节点方向
或要往叶子节点方向去,所获得最大权值(1表示往叶子节点,0往根节点)
有一点值得一提:就是如果u点有陷阱,那么dp[u][1][1]不能由dp[v][0][1]转移过来,因为一旦限制为1,那么就不能由u往叶子方向走了
这一点很难理解。我们在求最大值是会用到dp[u][0][1]的,怎么办呢?我们发现dp[u][0][1]是可以由dp[u][0][0]代替的。

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define Max 1000001
#define MAXN 1001009
#define MOD 1000000007
#define rin freopen("in.txt","r",stdin)
#define rout freopen("1.out","w",stdout)
#define Del(a,b) memset(a,b,sizeof(a))
typedef long long LL;
using namespace std;
const int N = 100005;
int T;
struct Node {
    int next;
    int to;
} e[MAXN];
int tot, ans, c;
int head[N], vis[N];
int dp[N][4][3];
int trap[N];
int gift[N];
void Init(int n) {
    Del(head, -1);
    ans = 0;
    tot = 0;
    Del(dp,0);
}
void addedge(int from, int to) {
    e[tot].to = to;
    e[tot].next = head[from];
    head[from] = tot++;
}
void dfs(int u, int father) {
    dp[u][trap[u]][0]=gift[u];
    dp[u][trap[u]][1]=gift[u];
    for (int i = head[u]; i != -1; i = e[i].next) {
        int v = e[i].to;
        if (v == father)
            continue;
        dfs(v, u);
        for (int j = 0; j <= c; j++) {
            for (int k = 0; j + k <= c; k++) {
                if (j != c)
                    ans = max(ans, dp[u][j][0] + dp[v][k][1]);
                if (k != c)
                    ans = max(ans, dp[u][j][1] + dp[v][k][0]);
                if (j + k < c)
                    ans = max(ans, dp[u][j][0] + dp[v][k][0]);  //起点和终点都可以为非陷阱
                if (k + j <= c)
                    ans = max(ans, dp[u][j][1] + dp[v][k][1]);  //起点和终点都可以为陷阱
            }
        }

        for (int j = 0; j + trap[u] <= c; j++) {
            dp[u][j + trap[u]][0] = max(dp[u][j + trap[u]][0],
                    dp[v][j][0] + gift[u]);
            if (j != 0) {
                dp[u][j + trap[u]][1] = max(dp[u][j + trap[u]][1],
                        dp[v][j][1] + gift[u]);
            }
        }
    }

}
int main() {
    int n, T;
    while (cin >> T) {
        while (T--) {
            scanf("%d%d", &n, &c);
            Init(n);
            for (int i = 0; i < n; i++) {
                scanf("%d%d", &gift[i], &trap[i]);
            }
            for (int i = 1; i < n; i++) {
                int x, y;
                scanf("%d%d", &x, &y);
                addedge(y, x);
                addedge(x, y);
            }
            dfs(0, -1);
            printf("%d\n", ans);
        }
    }
    return 0;
}
posted @ 2019-10-14 23:44  风骨散人  阅读(117)  评论(0编辑  收藏  举报