K - The Ghost Blows Light

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2015-10-20)

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) 
 

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
 
解题报告:
 因为是一棵树,所以起点到终点的路径已经确定了,所以我们可以先暴力出所有路径上的点。
这样路径就是一条链了,之后我们在这条链上每个点进行一次树形背包,然后点之间的转移依旧用背包,就可以得出最终答案了,可以预先处理处1->n的距离,判断无解情况.
 
#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的更多相关文章

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

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

  2. HDU 4276 The Ghost Blows Light (树形DP,变形)

    题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...

  3. 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 ...

  4. HDOJ 4276 The Ghost Blows Light

    题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...

  5. 【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 ...

  6. BNUOJ 26283 The Ghost Blows Light

    The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...

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

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

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

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

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

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

随机推荐

  1. spring mvc json 返回乱码问题解决(vestion:3.x.x)

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...

  2. 【winform程序】自定义webrowser控件调用IE的版本

    修改注册表: bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROW ...

  3. acid数据库事务正确执行的四个基本要素的缩写编辑本义项

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  4. WEB服务健康状态检测

    #!/bin/sh #date:2015-12-07 #filename:check_web.sh #作者:lixingli #Email:1162572407@qq.com #version:v1. ...

  5. linux-ln命令

    ln分为软链接和硬链接 1.软连接 -s   ln -s /mnt/hgfs/SHARE hvshare2 相当于在当前目录下新建一个名为hvshare2的快捷方式指向/mnt/hgfs/SHARE路 ...

  6. 动软代码生成V2.74模版简介

    最近发现很多人用动软代码生成,确实方便,有些经验记录下,以后查看回顾. ..\Maticsoft\Codematic2\Template\TemplateFile 为模板文件夹,直接在目录下新建文件夹 ...

  7. C# winCE5.0开发右键效果解决方案

    用VS2008开发C#语言wince程序,发现程序里右键捕获不到,采集器上点也没反应,上网查好像有个c++版本的,看不懂啊,下面我给出C#实现右键效果的解决方案,请各位多多优化. 首先控件Contex ...

  8. django: db - many to many

    本讲介绍数据库多对多关系,代码样例继前文使用. 一,在 blog/models.py 中创建对象: # Many-To-Many Example : Authors vs Books class Au ...

  9. git开源项目协作

    开源项目协作 fork开源项目,即打开开源项目的github,然后点击fork按钮 pull request

  10. HTML 5 与HTML 4 的区别

    (1)HTML 5 与HTML 4 的相比,语法的改变,以下四个方面: 字符编码改变举例: 省略标记值: (2)新增和废弃的元素 (3)新增html全局属性 (1)指定元素是否可编辑 (2)指定页面是 ...