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. Laravel5.2 下使用Form

    laravel到了5.1.*以上版本,便没有了illuminate/html类库的支持, 我试着把illuminate/html类库加入了laravel5.2,依然没有用, 但是laravelcoll ...

  2. oracle递归函数

    oracle start with connect by 使用方法 oracle中 connect by prior 递归算法  Oracle中start with...connect by prio ...

  3. 用java实现Simsimi小黄鸡接口

    package com.iask.webchat.chatmachine; import java.io.BufferedReader; import java.io.InputStream; imp ...

  4. 设计模式(3)-对象创建型模式-Abstract Factory模式

    1.对象创建型模式 1.3           Abstract Factory模式 1.3.1 需求 在下面情况能够使用Abstract Factory模式: •  一个系统要独立于它的产品的创建. ...

  5. 如何使用easyUI

    一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...

  6. [转]getResource()和getResourceAsStream以及路径问题

    原文链接:http://blog.sina.com.cn/s/blog_4b5bc0110100g22w.html 用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉 ...

  7. [c#]asp.net开发微信公众平台(5)微信图文消息

    上篇已经成功响应了关注事件,也实现了文本消息的发送,这篇开始图文消息处理, 微信中最常用的消息类型就是图文消息了,因为它图文并茂,最能表达信息. 图文消息在微信中的接口定义如下: <xml> ...

  8. EasyUi DataGrid中数据编辑方式及编辑后数据获取,校验处理

    EasyUi中的DataGrid提供前台编辑的相关函数. 实现客户选中DataGrid中一列后,对选中列中的一个字段进行编辑,并对数据进行实时校验后,传递至后台保存的需求, 主要涉及到DataGrid ...

  9. Winform控件缩写

    控件名称 缩写 Buttom按钮 Btn CheckBox复选框 Chk ColumnHeader视图列表头 Col ComboBox组合框 Cbo ContextMenu快捷菜单 Ctm DataG ...

  10. nginx入门手册(一)

    1.nginx进程: nginx会启动多个进程: 一个主进程Master. 几个工作进程worker. 缓存加载器进程 缓存管理器进程 master主要工作: 1. 读取并验正配置信息: 2. 创建. ...