hdoj 4276 The Ghost Blows Light 【树形dp】

题目链接:hdoj 4276 The Ghost Blows Light

The Ghost Blows Light

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3091 Accepted Submission(s): 956

Problem Description

My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N rooms (numbered from 1 to N) which are connected by some roads (pass each road should cost some time). There is exactly one route between any two rooms, and each room contains some treasures. Now I am located at the 1st room and the exit is located at the Nth room.
Suddenly, alert occurred! The tomb will topple down in T minutes, and I should reach exit room in T minutes. Human beings die in pursuit of wealth, and birds die in pursuit of food! Although it is life-threatening time, I also want to get treasure out as much as possible. Now I wonder the maximum number of treasures I can take out in T minutes.

Input
There are multiple test cases.
The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)

Output
For each test case, output an integer indicating the maximum number of treasures I can take out in T minutes; if I cannot get out of the tomb, please output “Human beings die in pursuit of wealth, and birds die in pursuit of food!”.

Sample Input
5 10
1 2 2
2 3 2
2 5 3
3 4 3
1 2 3 4 5

Sample Output
11

题意:给定一个树和树边的距离以及节点的权值,问你在T秒能否从1-n,如果可以到达输出T秒可以获得的最大权值。

思路:明显的树形dp,首先1 -> n的路径是确定的,我们可以先找出来。这样的话得到1 -> n的耗时time,将路径上的树边距离置0,下面就是求解T-time秒从节点1出发可以获得的最大权值。

设置dp[u][i]为从节点u出发花费时间i并回到u节点可以获得的最大权值。
t = dis(u, v) * 2。
dp[u][i] = max(dp[u][i], dp[u][k] + dp[v][i-k-t])

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct Edge {
    int from, to, val, next;
};
Edge edge[MAXN*2];
int head[MAXN], edgenum;
void init() { CLR(head, -1); edgenum = 0; }
void addEdge(int u, int v, int w) {
    Edge E = {u, v, w, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
void Solve(int u, int v) {
    for(int i = head[u]; i != -1; i = edge[i].next) {
        if(edge[i].to == v) {
            edge[i].val = 0; break;
        }
    }
}
int top, rec[110]; bool flag;
int dp[110][600], a[110], T, n;
void find_path(int u, int fa, int need) {
    if(flag) return ;
    if(u == n) {
        if(need <= T) {
            T -= need;
            flag = true;
        }
        for(int i = 0; i < top-1; i++) {
            int u = rec[i], v = rec[i+1];
            Solve(u, v); Solve(v, u);
        }
        return ;
    }
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(v == fa) continue;
        rec[top++] = v;
        find_path(v, u, need + edge[i].val);
        top--;
    }
}
void DFS(int u, int fa) {
    for(int i = 0; i <= T; i++) dp[u][i] = a[u];
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(v == fa) continue;
        DFS(v, u);
        int t = edge[i].val * 2;
        for(int j = T; j >= 0; j--) {
            for(int k = j-t; k >= 0; k--) {
                //cout << u << ' ' << k << ' ' << dp[u][k] << ' ' << dp[v][j-k-t] << endl;
                dp[u][j] = max(dp[u][j], dp[u][k] + dp[v][j-k-t]);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d", &n, &T) != EOF) {
        init();
        for(int i = 1; i < n; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            addEdge(u, v, w);
            addEdge(v, u, w);
        }
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        flag = false; top = 0; rec[top++] = 1; find_path(1, -1, 0);
        if(!flag) {
            printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
            continue;
        }
        CLR(dp, 0); DFS(1, -1);
        printf("%d\n", dp[1][T]);
    }
    return 0;
}

你可能感兴趣的:(hdoj 4276 The Ghost Blows Light 【树形dp】)