BNUOJ 26283 The Ghost Blows Light
The Ghost Blows Light
This problem will be judged on HDU. Original ID: 4276
64-bit integer IO format: %I64d      Java class name: Main
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
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
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
#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的更多相关文章
- 【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 ...
 - HDU 4276 The Ghost Blows Light
		
K - The Ghost Blows Light Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
 - HDU4276 The Ghost Blows Light(树形DP+背包)
		
题目大概说一棵n个结点树,每个结点都有宝藏,走过每条边要花一定的时间,现在要在t时间内从结点1出发走到结点n,问能获得最多的宝藏是多少. 放了几天的题,今天拿出来集中精力去想,还是想出来了. 首先,树 ...
 - 树形DP(01组合背包The Ghost Blows Light  HDU4276)
		
题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...
 - HDU-4276 The Ghost Blows Light (树形DP+背包)
		
题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...
 - HDU4276 The Ghost Blows Light SPFA&&树dp
		
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...
 - HDU 4276-The Ghost Blows Light(树状背包)
		
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
 - HDU4276 - The Ghost Blows Light(树形DP)
		
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
 - HDU 4276 The Ghost Blows Light(树形)
		
题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...
 
随机推荐
- [CF1076F] Summer Practice Report
			
Description Transmission Gate Solution 这一题可以考虑Dp,设\(Dp[i][j]\) 为在第i段中,以j颜色为结尾的最后一小段长度的最小值. 那么可以先考虑以表 ...
 - 二分查找+数学 HDOJ 4342 History repeat itself
			
题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...
 - 找规律 UVALive 6506 Padovan Sequence
			
题目传送门 /* 找规律:看看前10项就能看出规律,打个表就行了.被lld坑了一次:( */ #include <cstdio> #include <algorithm> #i ...
 - 表达式语言EL简单学习
			
Jsp2.0最重要的特性就是表达式语言EL.jsp用户可以用它来访问应用程序数据. EL表达式以${开头并以}结束. ${expresion} ${x+y} 它也常用来连接两个表达式,取值将从 ...
 - framework7 点取消后还提交表单解决方案
			
$$('form.ajax-submit').on('submitted', function (e) { var xhr = e.detail.xhr; // actual XHR object v ...
 - jsp中非空判断
			
function中uname要和id的值相匹配,但是这样不专业,要显示我的专业性,我将使用document获得name的值来判断是否为空,应为这样是专业的写法,我要时刻记住我是专业的 <scri ...
 - CF949A/950C Zebras
			
思路: 贪心乱搞. 实现: #include <bits/stdc++.h> using namespace std; vector<vector<int>> v; ...
 - 2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
			
思路: 状压dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
 - 'NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value coding compliant
			
解决一个问题: 当我添加一个IBout, 报了如下错误 NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value codi ...
 - mysql 5.7安装过程中,初始化的问题
			
初始化不指定参数文件,如使用以下命令初始化: ./mysqld --initialize --user=mysql --basedir=/data/mysql/barry_mysql --datadi ...