The Ghost Blows Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1017 Accepted Submission(s): 303
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)
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
题意:一张无向图,每条边走过去要花一定时间,每个节点上有一定数量的宝藏,在规定时间必须由起点走到终点,并尽可能多拿宝藏
分析:此题分两步做,首先求最短路,然后把这条路上的所有边的花费时间,再把总的时间减去上述时间
这样做的原因是最短路方法求出来的路是必经之路
然后做树形DP,以DFS的方法从起始点开始深搜,每搜到一个点x就更新一次所有dp[x][i](指到x点时还剩i时间,这时最多能已经拿到多少宝藏,i从m到2倍到下个点的时间)
题意:一张无向图,每条边走过去要花一定时间,每个节点上有一定数量的宝藏,在规定时间必须由起点走到终点,并尽可能多拿宝藏
分析:此题分两步做,首先求最短路,然后把这条路上的所有边的花费时间,再把总的时间减去上述时间
这样做的原因是最短路方法求出来的路是必经之路
然后做树形DP,以DFS的方法从起始点开始深搜,每搜到一个点x就更新一次所有dp[x][i](指到x点时还剩i时间,这时最多能已经拿到多少宝藏,i从m到2倍到下个点的时间)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
int n, m;
const int INF = 1 << 31 - 1;
struct node{
int to;
int next;
int weight;
}e[201];
int w[210];
int lastshow[210];
int dp[210][510]; //dp[x][i]表示从x出发时间还剩m的时候最多拿多少宝藏
bool inqueue[2010];
queue<int>q;
long long d[210];
int cnt = -1;
int used[210];
int path[210];
void insert(int a, int b, int w){
e[++cnt].to = b; //插入边,起始编号为0
e[cnt].weight = w;
e[cnt].next = lastshow[a];
lastshow[a] = cnt;
}
bool spfa(){
q.push(1);
int qq[2005];
while(!q.empty()){
int x = q.front();
q.pop();
inqueue[x] = false;
int id = lastshow[x];
while(id != -1){
if(d[x] < INF && d[e[id].to] > e[id].weight + d[x]){
d[e[id].to] = e[id].weight + d[x];
path[e[id].to] = x; //记录一路上经过的点
qq[e[id].to] = id; //记录以上述点为终点的边
if(!inqueue[e[id].to]){
inqueue[e[id].to] = true;
q.push(e[id].to);
}
}
id = e[id].next;
}
}
int i;
for(i = n; path[i] != -1; i = path[i]){ //特别注意此处判断循环停止的条件是path[i]!=-1,若是i!=-1,会强制删除边0
e[qq[i]].weight = 0;
}
return false;
}
void init(){
int i;
memset(used, 0, sizeof(used));
memset(path, -1, sizeof(path));
memset(dp, 0, sizeof(dp));
memset(lastshow, -1, sizeof(lastshow));
for(i = 1; i <= n; i ++){
inqueue[i] = false;
}
for(i = 1; i <= n; ++ i)
d[i]=INF;
d[1]=0;
cnt=-1;
while(!q.empty()){
q.pop();
}
}
void dfs(int x){//树形dp
used[x] = 1;
for(int k = lastshow[x]; k!= -1; k = e[k].next){
int y = e[k].to;
if(used[y])
continue;
dfs(y); //每到一个新的点就深度遍历此点
int tmp = e[k].weight * 2;
for(int i = m; i >= tmp; i --){
for(int j = i - tmp; j >= 0; j --){
dp[x][i] = max(dp[x][i], dp[x][j] + dp[y][i - tmp - j]);
}
}
}
for(int i = 0; i <= m; i ++){
dp[x][i] += w[x];//更新所有以x为起始的情况下能获得的宝藏数
}
}
int main(){
int i, j, k;
while(~scanf("%d %d", &n, &m)){
int a, b, c;
init();
for(i = 0; i < n - 1; i ++){
scanf("%d %d %d", &a, &b, &c);
insert(a, b, c);
insert(b, a, c);
}
for(i = 1; i <= n; i ++){
scanf("%d", &w[i]);
}
spfa(); //找出最短路,并把路上要花费的时间置为零,因为它是必经之路
//cout << d[n] << endl;
if(d[n] > m){
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
}
else{
m -= d[n]; //更新还剩的时间
dfs(1);
printf("%d\n", dp[1][m]);
}
}
return 0;
}