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. mac 环境下使用virtual box 虚拟机(win7)与主机之间互相ping通

    首先选择virtual box设置网络连接方式为网桥 混杂模式设置为全部允许 如下图: 进入虚拟机把虚拟机IP设置和主机在一个网段.如主机是192.168.1.100虚拟机可以设置为192.168.1 ...

  2. java.lang.NoSuchFieldError: INSTANCE

    java.lang.NoSuchFieldError: INSTANCE异常,可能是包重复了. 我遇到的情况是maven里引入了一个JAR,而我又在lib里面引入了这个jar,并且版本还不相同,就出了 ...

  3. Quartz中时间表达式的设置-----corn表达式

    Quartz中时间表达式的设置-----corn表达式 时间格式: <!-- s m h d m w(?) y(?) -->,   分别相应: 秒>分>小时>日>月 ...

  4. 2013国内IT行业薪资对照表【技术岗】

    (本文为转载,具体出处不详) 说薪水,是所有人最关心的问题.我只 想说如果想在薪水上面满意,在中国,没有哪里比垄断国企好.电力.烟草.通信才是应该努力的方向.但是像我们这种搞研发的进IT行业似乎是注定 ...

  5. Oracle11g主要服务程序

    Oracle Orcl VSS Writer Service:Oracle对 VSS(卷影)的支持服务.Oracle卷映射拷贝写入服务VSS(Volume Shadow Copy Service)能够 ...

  6. web_api vs2015 新加标题无法打开

    HomeController 去掉特性[Authorize]

  7. django: db - admin

    本讲演示简单使用 Django Admin 功能. 一,修改 settings.py,添加 admin 应用: INSTALLED_APPS = ( 'django.contrib.auth', 'd ...

  8. NHibernate之映射文件配置说明(转载3)

    十二.组件(component), 动态组件(dynamic-component) <component>元素把子对象的一些元素与父类对应的表的一些字段映射起来. 然后组件可以定义它们自己 ...

  9. 回顾:Linq To SQL语法 - 实体类

    第一篇博客,还望各位大神勿喷 小弟在此代码奉上........ 借用NorthWind数据库,实现一个商品展示的小功能.上代码: 添加对Linq的引用 using System.Data.Linq;/ ...

  10. linux添加JAVA环境变量

    root用户: 1.修改文件vim /etc/profile 添加以下信息: export JAVA_HOME=/home/jdk1..0_79 (这里需要添加自己的JDK安装目录) export C ...