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
Source
2012 ACM/ICPC Asia Regional Changchun Online
解题思路:由题意我们知道从1到n的路径上的边仅经过一次,其余的边要么不经过要么经过两次,因此我们可以首先将1->n路径上的边的权值设为0,这样我们利用分组背包的树形DP进行求解时是可以保证这条路径上的边肯定经过且仅经过一次。
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
int n, T, u, v, w;
struct Edge {
int u, v, w, next;
Edge() { }
Edge(int _u, int _v, int _w, int _next) : u(_u), v(_v), w(_w), next(_next) { }
}edges[2*maxn];
int head[maxn], edge_sum;
void init_graph() {
edge_sum = 0;
memset(head, -1, sizeof(head));
}
void add_edge(int u, int v, int w) {
edges[edge_sum].u = u;
edges[edge_sum].v = v;
edges[edge_sum].w = w;
edges[edge_sum].next = head[u];
head[u] = edge_sum++;
edges[edge_sum].u = v;
edges[edge_sum].v = u;
edges[edge_sum].w = w;
edges[edge_sum].next = head[v];
head[v] = edge_sum++;
}
int node[maxn];
int pre[2*maxn];
int dp[maxn][510];
void dfs1(int u, int fa) {
for(int i = head[u]; i != -1; i = edges[i].next) {
int v = edges[i].v;
if(v == fa) continue;
pre[v] = i;
dfs1(v, u);
}
return ;
}
void dfs2(int u, int fa) {
for(int i = 0; i <= T; ++i) {
dp[u][i] = node[u];
}
for(int i = head[u]; i != -1; i = edges[i].next) {
int v = edges[i].v;
int w = edges[i].w;
if(v == fa) continue;
dfs2(v, u);
for(int j = T; j >= 0; --j) {
for(int k = 0; k + 2*w <= j; ++k) {
dp[u][j] = max(dp[u][j], dp[u][j-k-2*w] + dp[v][k]);
}
}
}
return ;
}
void solve() {
int tot_w = 0;
memset(pre, -1, sizeof(pre));
dfs1(1, -1);
int id = pre[n];
while(id != -1) {
tot_w += edges[id].w;
edges[id].w = 0;
edges[id^1].w = 0;
id = pre[edges[id].u];
}
if(tot_w > T) {
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
} else {
T -= tot_w;
dfs2(1, -1);
printf("%d\n", dp[1][T]);
}
return ;
}
int main() {
//freopen("aa.in", "r", stdin);
while(scanf("%d %d", &n, &T) != EOF) {
init_graph();
for(int i = 1; i < n; ++i) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
}
for(int i = 1; i <= n; ++i) {
scanf("%d", &node[i]);
}
solve();
}
return 0;
}