HDU 4276 The Ghost Blows Light
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-10-20)
Description
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
1 2 2
2 3 2
2 5 3
3 4 3
1 2 3 4 5
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int maxn = 1e2 + 15;
struct Edge {int v,nxt,w;};
Edge e[maxn*5];
int n , T , head[maxn] , tot , val[maxn] , dp[maxn][maxn*5] , ban[maxn] , ans[2][maxn*5] , mat[maxn][maxn];
vector< int >path; void addedge(int u ,int v,int w) {e[tot].v=v,e[tot].nxt=head[u],e[tot].w=w,head[u]=tot++;} bool dfs_path(int u,int fa)
{
if(u==n)
{
path.push_back(u);
return true;
}
for(int i = head[u] ; ~i ; i = e[i].nxt)
{
int v = e[i].v;
if(v==fa) continue;
if(dfs_path(v,u))
{
path.push_back(u);
return true;
}
}
return false;
} inline void updata(int & x ,int v)
{
x = max( x , v);
} void dfs(int u ,int fa)
{
for(int i = 0 ; i <= T ; ++ i) dp[u][i] = val[u];
for(int i = head[u] ; ~i ; i = e[i].nxt)
{
int v = e[i].v;
if(v == fa || ban[v]) continue;
dfs( v , u );
for(int j = T ; j >= 0 ; -- j)
{
int w = e[i].w;
for(int k = T - j - w * 2 ; k >= 0 ; -- k) updata( dp[u][j + k + w * 2] , dp[u][j] + dp[v][k]);
}
}
} int main(int argc,char *argv[])
{
while(~scanf("%d%d",&n,&T))
{
memset(head,-1,sizeof(head));tot=0;path.clear();memset(ban , 0 , sizeof(ban));memset( ans , 0 , sizeof(ans) ); int costall=0;
int cur = 0;
for(int i = 1 ; i < n ; ++ i)
{
int u , v , w;scanf("%d%d%d",&u,&v,&w);
addedge( u , v , w); addedge( v , u , w);
mat[u][v] = mat[v][u] = w;
}
for(int i = 1 ; i <= n ; ++ i) scanf("%d",val+i);
dfs_path(1,0) ; reverse(path.begin() , path.end());
for(int i = 0 ; i < path.size() - 1 ; ++ i) costall += mat[path[i]][path[i+1]];
if(costall > T)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
costall = 0;
for(int i = 0 ; i < path.size() - 1 ; ++ i)
{
int cost = mat[path[i]][path[i+1]];
ban[path[i]]=1;
ban[path[i+1]] = 1;
int pre = cur ; cur ^= 1;memset(ans[cur] , 0 , sizeof(ans[cur]));
int u = path[i];
dfs( u , -1);
for(int j = T ; j >= costall ; -- j)
for(int k = T - j - cost; k >=0 ; -- k)
updata( ans[cur][j + k + cost] , ans[pre][j] + dp[u][k]);
costall += cost;
}
int res = 0;
dfs( n , -1);
for(int i = T ; i >= costall ; -- i)
for(int j = T - i ; j >= 0 ; -- j)
res = max( res , ans[cur][i] + dp[n][j]);
printf("%d\n",res);
}
return 0;
}
HDU 4276 The Ghost Blows Light的更多相关文章
- HDU 4276 The Ghost Blows Light(树形)
题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...
- HDU 4276 The Ghost Blows Light (树形DP,变形)
题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...
- HDOJ 4276 The Ghost Blows Light(树形DP)
Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N room ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
- 【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 ...
- BNUOJ 26283 The Ghost Blows Light
The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...
- HDU 4276-The Ghost Blows Light(树状背包)
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
- 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房间出去,保证再不炸死 ...
随机推荐
- 修改cmd的字体
通常打开的cmd默认的字体比较小,字体只有宋体和新宋体两种,如果要修改,需要通过修改注册表才行. 打开regedit后,找到如下路径HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
- ORACLE REFERENCES FRO TEST
[JSU]LJDragon's Oracle course notes In the first semester, junior year Oracle考前复习 试题结构分析: 1.选择题2x10, ...
- JS~模拟表单在新窗口打开,避免广告拦截
说起广告拦截,这应该是浏览器的一个特性,它会将window.open产生的窗口默认为一个广告,将它进行拦截,但有时,这不是我们所希望的,有时,我们就是需要它在客户端的浏览器上弹出一个新窗口,以展示数据 ...
- JavaScript新手学习笔记3——三种排序方式(冒泡排序、插入排序、快速排序)
每种编程语言学到数组的时候,都会讲到排序算法,当时学C语言的时候,卡在排序算法.今天来总结一下javascript中如何实现三种排序算法. 1.冒泡排序(默认升序排列哦) 原理: 冒泡排序的原理,顾名 ...
- 【录音】Android录音--AudioRecord、MediaRecorder
Android提供了两个API用于实现录音功能:android.media.AudioRecord.android.media.MediaRecorder. 网上有很多谈论这两个类的资料.现在大致总结 ...
- Python - SQLAlchemy之连表操作
ORM的两种创建方式 数据库优先:指的是先创建数据库,包括表和字段的建立,然后根据数据库生成ORM的代码,它是先创建数据库,再创建相关程序代码 代码优先:就是先写代码,然后根据代码去生成数据库结构. ...
- Log4net日志组件使用
一.简介 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的日志系统来诊断和修复配置上的问题.经验表明,日 ...
- OpenXML_导入Excel到数据库
(1).实现功能:通过前台选择.xlsx文件的Excel,将其文件转化为DataTable和List集合 (2).开发环境:Window7旗舰版+vs2013+Mvc4.0 (2).在使用中需要用到的 ...
- 多线程、多任务管理 简单demo
需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况): 多个任务 根据条件不同 可能需要不同的处理 分析: 多线程并发执行多任务: 对任务进行管理,追踪中间执行状态: 运用 ...
- C# 前台线程与后台线程区别
using System; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace Wi ...