The Ghost Blows Light

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4276
64-bit integer IO format: %I64d      Java class name: Main

 
 
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

 
 
解题:树形dp...此题要求从1 入从N出,由于是树,故只有一条路径从1通往N.只需要求出这题路径上的时间和,并且将时间置为0.然后在总时间中减去这部分时间再进行dp.为什么要求和呢?首要原因是判断能否活命!人为财死,鸟为食亡。
有人去求最短路!我只想说,只有一条路径,求最短路有意义吗?
 
如果这题路径上的时间和比给出的时间还大,必死无疑!为什么要置0呢?因为这部分的时间是必须要花掉的!!!!!不然还活不活啊?置零后减去就好了!由于不需花费时间,这些边,根据dp的设计,这些路径上的财富一定会被拿掉的!因为一直取max啊!不要任何消耗就能更大!有点贪心啊
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,w;
};
int dp[maxn][],val[maxn],n,t,sum;
vector<arc>g[maxn];
bool dfs1(int u, int fa){
if(u == n) return true;
for(int i = ; i < g[u].size(); i++){
if(g[u][i].to == fa) continue;
if(dfs1(g[u][i].to,u)){
sum += g[u][i].w;
g[u][i].w = ;
return true;
}
}
return false;
}
void dfs(int u,int fa) {
for(int i = ; i <= t; i++) dp[u][i] = val[u];
for(int v = ; v < g[u].size(); v++) {
if(g[u][v].to == fa) continue;
dfs(g[u][v].to,u);
int cost = *g[u][v].w;
for(int j = t; j >= cost; j--){
for(int k = ; k <= j - cost; k++)
dp[u][j] = max(dp[u][j],dp[u][j-k-cost]+dp[g[u][v].to][k]);
}
}
}
int main() {
int u,v,w,i;
while(~scanf("%d %d",&n,&t)) {
for(i = ; i <= n; i++)
g[i].clear();
for(i = ; i < n; i++) {
scanf("%d %d %d",&u,&v,&w);
g[u].push_back((arc) {v,w});
g[v].push_back((arc) {u,w});
}
for(i = ; i <= n; i++)
scanf("%d",val+i);
memset(dp,,sizeof(dp));
sum = ;
dfs1(,-);
if(sum > t){
puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!");
continue;
}
t -= sum;
dfs(,-);
cout<<dp[][t]<<endl;
}
return ;
}

BNUOJ 26283 The Ghost Blows Light的更多相关文章

  1. 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

    The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...

  2. HDU 4276 The Ghost Blows Light

    K - The Ghost Blows Light Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  3. HDU4276 The Ghost Blows Light(树形DP+背包)

    题目大概说一棵n个结点树,每个结点都有宝藏,走过每条边要花一定的时间,现在要在t时间内从结点1出发走到结点n,问能获得最多的宝藏是多少. 放了几天的题,今天拿出来集中精力去想,还是想出来了. 首先,树 ...

  4. 树形DP(01组合背包The Ghost Blows Light HDU4276)

    题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...

  5. HDU-4276 The Ghost Blows Light (树形DP+背包)

    题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...

  6. HDU4276 The Ghost Blows Light SPFA&&树dp

    题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...

  7. HDU 4276-The Ghost Blows Light(树状背包)

    题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...

  8. HDU4276 - The Ghost Blows Light(树形DP)

    题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...

  9. HDU 4276 The Ghost Blows Light(树形)

    题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...

随机推荐

  1. ACM复习专项

    资料整理 ACM训练营 邝斌的ACM模板 牛客网哈理工ACM教学视频 视频网盘资料(密码:kntr) 1. 训练阶段 第一阶段:练习经典常用算法 (本周任务) 1. 最短路(Floyd.Dijstra ...

  2. Minimal Ratio Tree HDU - 2489

    Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...

  3. Apusic中间件结合MyEclipse进行远程调试记录

    Apusic中间件结合MyEclipse进行远程调试记录. 在金蝶域中正常部署应用. 启动金蝶中间件时使用"startapusic -ds"命令. 在MyEclipse的Run-- ...

  4. Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法

    Specifying the Code to Run on a Thread 上一课   下一课 1.This lesson teaches you to Define a Class that Im ...

  5. C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

    原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...

  6. Codeforces Round #230 (Div. 1)

    A: 题意:给你一个半径为n的圆 求最少阻塞多少个点 才能使所以圆内及圆上的点 都不与外边的点相连  相连是距离为1 只算整数点 这题定住x,y依次递减 判断一下是否4-connect 这个意思就是 ...

  7. 在spring boot 中使用itext和itextrender生成pdf文件

    转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...

  8. Android ImageView setImageBitmap 不显示图片

    从sd卡里读出图片后有时调用setImageBitmap(bitmap)方法会显示不出图片,仔细考虑过后原来是加载的图片过大导致的,解决办法为: BitmapFactory.Options op = ...

  9. Android学习笔记(八) CheckBox和RadioGroup

    一.CheckBox 1.CheckBox的常用方法: boolean isChecked() :返回当前CheckBox的选中状态 void setChecked(boolean isChecked ...

  10. iOS Programming Views :Redrawing and UIScrollView

    iOS Programming Views :Redrawing and UIScrollView  1.1 event  You are going to see how views are red ...