#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1E2 + 10;
int n, T, t, a, b, c, dp[maxn][maxn * 5], head[maxn], cnt, ans;
struct Edge
{
int from, to, cost, pre;
Edge(int a = 0, int b = 0, int c = 0, int d = 0): from(a), to(b), cost(c), pre(d) {}
};
Edge edge[maxn * 5];
void addEdge(int a, int b, int c)
{
edge[cnt] = Edge(a, b, c, head[a]);
head[a] = cnt++;
}
int spfa(int st, int ed)
{
queue<int>que;
int vis[maxn], dis[maxn], pre[maxn];
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
memset(pre, -1, sizeof(pre));
que.push(st);
vis[st] = 1; dis[st] = 0;
while (!que.empty())
{
int u = que.front();
que.pop(); vis[u] = 0;
for (int i = head[u]; i != -1; i = edge[i].pre)
{
int v = edge[i].to, cost = edge[i].cost;
if (dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = 1;
que.push(v);
}
}
}
}
for (int i = ed; i != st; i = edge[pre[i]].from)
{
edge[pre[i]].cost = 0;
edge[pre[i] ^ 1].cost = 0;
}
return dis[ed];
}
void dfs(int u, int fa, int coin)
{
for (int e = head[u]; e != -1; e = edge[e].pre)
{
int v = edge[e].to, cost = edge[e].cost * 2;
if (v == fa || coin < cost) continue;
dfs(v, u, coin - cost);
for (int i = coin; i >= cost; i--)
for (int j = 0; j + cost <= i; j++)
if (dp[u][i - j - cost] != -1 && dp[v][j] != -1)
dp[u][i] = max(dp[u][i], dp[u][i - j - cost] + dp[v][j]);
}
}
int main(int argc, char const *argv[])
{
while (~scanf("%d%d", &n, &T) && n + T)
{
ans = 0; cnt = 0;
memset(dp, -1, sizeof(dp));
memset(head, -1, sizeof(head));
for (int i = 1; i < n; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c); addEdge(b, a, c);
}
for (int i = 1; i <= n; i++) scanf("%d", &dp[i][0]);
T -= spfa(1, n);
if (T < 0) printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
else
{
dfs(1, 0, T);
for (int i = 0; i <= T; i++) ans = max(ans, dp[1][i]);
printf("%d\n", ans);
}
}
return 0;
}
给你n个点,n-1条边构成树,每条边有边树,每个点有点权(表示走每条边的时间),问在时间T从点1走到点n,能够得到最多的点权有多少。
先跑出最短路,时间大于T,无解。最短路径上的边只会经过一次且必须经过一次,而其他边会经过两次。把最短路径上的边置为0,使得dp必须经过这些边,dp[p][i]=max(dp[p][i],dp[t][i-w])