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. Servlet问题:servlet cannot be resolved to a type解决办法

    工程里的路径权限高,并且eclipse并到classpath里寻找jar位置,所以我就到我的java项目里 项目名-->右键 Property-->选择 Java Build Path-- ...

  2. SPOJ 416 - Divisibility by 15(贪心)

    糟烂的代码啊...  这个题目思路很简单——末位只可能为0和5,所有数字的和肯定被3整除 没有0和5的肯定不行 否则,把所有数字求和 如果被3整除,则从大到小输出 如果除3余1,则按以下顺序——删1: ...

  3. java泛型中? super T和? extends T的区别

    <? super T>表示包括T在内的任何T的父类,<? extends T>表示包括T在内的任何T的子类;请记住PECS原则:生产者(Producer)使用extends,消 ...

  4. 32位PLSQL_Developer连接oracle11g_64位

    1. 请将你下载的instantclient-basic-win32-10.2.0.5 文件解压.然后复制到你的数据库安装的文件夹下的producti文件夹下,我的是: E:\app\Administ ...

  5. JavaScript js无间断滚动效果 scrollLeft方法 使用模板

    JavaScript js无间断滚动效果 scrollLeft方法 使用模板 <!DOCTYPE HTML><html><head><meta charset ...

  6. 一、webpack那点事-安装、环境搭建

    前言: 还记得两年前刚来公司才几个月,经理就安排我去做JS地图相关的维护和开发工作,然后就跟着一个公司老鸟(没俩月他离职了)熟悉地图相关的功能. 本人嘛,那会前端JS实际开发经验也才几个月,然后当我看 ...

  7. Objective-C中的copy协议

    NSObject对象是否可以copy自己 NSObject类没有实现NSCopying或者NSMutableCopying协议,但是却有copy以及mutableCopy实例方法.然而,如果用NSOb ...

  8. 求解:C#.Net 远程方法调用失败 (Exception from HRESULT: 0x800706BE)

    服务器:Windows Server2003 sp2服务器 客户端:XP SP3 内容:C#Winform客户端调用服务器的Excel模板生成报表的时候,生成失败,抛出的异常如下: TargetInv ...

  9. mysql 存储过程与函数

    即事先经过编译并存储在数据库中的一段sql语言. 一.创建函数 创建格式: CREATE FUNCTION sp_name ([func_parameter[,...]]) RETURNS type ...

  10. C#使用log4net

    C#很多异步机制使程序无法使用断点调试,这时候我们就需要使用日志输出.使用log4net一定要先引用net4log这个dll,不然无法使用. 作者很懒,直接上代码吧. using System; us ...