【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
The Ghost Blows Light
Problem DescriptionMy 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.InputThere 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)OutputFor 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 Input5 10
1 2 2
2 3 2
2 5 3
3 4 3
1 2 3 4 5Sample Output11Source
【题意】
一个有 N 个节点的树形的地图,知道了每条边经过所需要的时间,现在给出时间T,问能不能在T时间内从 1号节点到 N 节点。每个节点都有相对应的价值,而且每个价值只能被取一次,问如果可以从1 号节点走到 n 号节点的话,最多可以取到的最大价值为多少。
【分析】
这题跟poj2486类似,不过这题的问题是不知道是否回到n。
规定回到n的话,我们画一下图会发现,我们走的路径是1到n的路径只走一遍,其他路径一定是去和回的两遍。所以我们可以先求出1~n的路径,然后把这些边权置为0,然后就是依赖背包问题,f[i][j]表示在i这棵子树上走j分钟的最大价值,我们走一条边的费用是*2的,因去和回是两遍,最后要加上1~n的路径的代价。
(网上打的都是树形0-1背包是n^3,依赖背包可以打成n^2)
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 110
#define Maxm 510 struct node
{
int x,y,c,next;
}t[Maxn*];int len;
int first[Maxn],w[Maxn]; int mymax(int x,int y) {return x>y?x:y;} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int n,v;
int dis[Maxn],sum; bool dfs(int x,int fa)
{
if(x==n) return ;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
dis[y]=dis[x]+t[i].c;
if(dfs(y,x)) {sum+=t[i].c;t[i].c=;return ;}
}
return ;
} int f[Maxn][Maxm];
void ffind(int x,int fa)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
for(int j=;j<=v-*t[i].c;j++) if(f[x][j]!=-)
{
f[y][j+*t[i].c]=mymax(f[y][j+*t[i].c],f[x][j])+w[y];
}
ffind(y,x);
for(int j=;j<=v;j++) if(f[y][j]!=-)
{
f[x][j]=mymax(f[x][j],f[y][j]);
}
}
} int main()
{
while(scanf("%d%d",&n,&v)!=EOF)
{
len=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
for(int i=;i<=n;i++) scanf("%d",&w[i]);
sum=;
dfs(,);
if(sum>v)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
}
else
{
memset(f,-,sizeof(f));
f[][]=w[];
ffind(,);
int ans=;
for(int i=;i<=v-sum;i++) ans=mymax(ans,f[][i]);
printf("%d\n",ans);
} }
return ;
}
[HDU 4276]
2016-10-18 10:51:20
【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)的更多相关文章
- 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 ...
- HDU 4276 The Ghost Blows Light
K - The Ghost Blows Light Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- 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,而且有可 ...
- HDU4276 - The Ghost Blows Light(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
- HDU 4276-The Ghost Blows Light(树状背包)
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
- 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 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- JavaWeb_Day10_学习笔记1_response(3、4、5、6、7、8、9)发送状态码、响应、重定向、定时刷新、禁用浏览器缓存、响应字节数据、快捷重定向方法、完成防盗链
今天学习重点: 1.response和request响应和应答分别学习: 请求响应流程图 response 1 response概述 response是Servlet.service方法 ...
- 三、使用Maven构建简单的java项目
前边,我刚搭建了Maven环境,还有给大家推荐了学习资源,这个小节,我们来就来,,简单的玩玩maven. 1.所需工具: 1.Eclipse 2.apache-maven-3.3.9 3. ...
- Win7上安装oracle中可能遇到的错误
安装oracle,总是出现一个警告两个错误错误,其描述是:OUI-18001:不支持操作系统’Windows Vista版本6.1’,找了好久,终于找到原因,因为oracle不支持Win7操作系统. ...
- .NET 设计模式之单例模式(一)
1.简单解释:在创建对象时,无论创建多少次,在堆空间上只会申请一次内存空间. 2.例子(1): public class Singleton{ private static Singleton _si ...
- LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)
Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...
- Codevs 1191 数轴染色
1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- Kali linux网络配置
Kali linux 安装完成后,需要对其网络进行配置.使用DHCP服务是配置网卡最简单的方法之一,但渗透测试时通常不会这样做,因为系统会被记录在DHCP服务器的数据库中. 1 动态DHCP方式 配 ...
- sed工具使用
sed命令使用形式 1.sed命令从管道中读取数据处理 command | sed ' edit command' 通过管道把一个命令的标准输出读入到sed的标准输入,sed就起到了过滤作用 2.se ...
- SVG
目前SVG在国内的使用并不常见,并且关于svg的相关js库也不多,这里指出两款svg的库Snap.svg和svg.js,Snap.svg张鑫旭的博客上有关于他的使用APi http://www.zha ...