树形DP(01组合背包The Ghost Blows Light HDU4276)
题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死的情况下可以带走的最大价值的珠宝是多少?
分析:很容易想到,这是树上的01背包,可以抽象的想,要是珠宝价值最大即满足:1到n的路径必须经过,且经过一次,而与该路径相连的副路径要么走0次要么走2次 (因为还有回来),所以用DP[i][j]表示以i为根节点的子树,花费j的时间可以获得的最大价值,需要特殊处理的是1-n经过的路径时间花费全部设为0,然后以1为根节点dfs,最后DP[1][T-ans]+value[1]就是最大值,下面是动态方程:
for(int j=cost;j>=edge[i].w*2;j--)
{
for(int k=0;k<=j-edge[i].w*2;k++)
dp[u][j]=max(dp[u][j],dp[u][j-edge[i].w*2-k]+value[v]+dp[v][k]);//u为子树是经过j时间获得的最大价值
}
程序:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-4
#define inf 0x3f3f3f3f
#define M 209
#define PI acos(-1.0)
using namespace std;
struct node
{
int u,v,w,next;
}edge[M*2];
int t,head[M],fa[M],pre[M],dp[M][555],value[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dfs(int u,int f)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==f)continue;
fa[v]=u;
pre[v]=i;
dfs(v,u);
}
}
void dfs1(int u,int f,int cost)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==f||cost-edge[i].w*2<0)continue;
dfs1(v,u,cost-edge[i].w*2);
for(int j=cost;j>=edge[i].w*2;j--)
{
for(int k=0;k<=j-edge[i].w*2;k++)
dp[u][j]=max(dp[u][j],dp[u][j-edge[i].w*2-k]+value[v]+dp[v][k]);
}
}
}
int main()
{
int n,T,i,u;
while(scanf("%d%d",&n,&T)!=-1)
{
init();
for(i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(i=1;i<=n;i++)
scanf("%d",&value[i]);
fa[1]=-1;
dfs(1,1);
int ans=0;
for(u=n;u!=1;u=fa[u])
{
ans+=edge[pre[u]].w;
edge[pre[u]].w=edge[pre[u]^1].w=0;
}
T-=ans;
if(T<0)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
memset(dp,0,sizeof(dp));
dfs1(1,1,T);
printf("%d\n",dp[1][T]+value[1]);
}
}
树形DP(01组合背包The Ghost Blows Light HDU4276)的更多相关文章
- 【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 ...
- HDU 4276 The Ghost Blows Light
K - The Ghost Blows Light Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- BNUOJ 26283 The Ghost Blows Light
The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...
- 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-4661 Message Passing 树形DP,排列组合
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4661 题意:有n个人呈树状结构,每个人知道一个独特的消息.每次可以让一个人将他所知的所有消息告诉和他相 ...
- 树形DP+(分组背包||二叉树,一般树,森林之间的转换)codevs 1378 选课
codevs 1378 选课 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 学校实行学分制.每门的必修课都有固定的学分 ...
- CH5402 选课【树形DP】【背包】
5402 选课 0x50「动态规划」例题 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了 N(N≤300) 门的选修课程,每个学生可选课程的数量 M 是 ...
- HDU 4276 The Ghost Blows Light(树形)
题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
随机推荐
- 纯css下拉菜单的制作
通过观察下拉菜单的过程,发现有两种状态,一种是鼠标没有hover时,一种是鼠标hover时. 主菜单hover时,显示子菜单:主菜单没有hover时,不显示子菜单 <!DOCTYPE html& ...
- Qt调用Server SQL中的存储过程
Server SQL中的存储过程如下: CREATE procedure PINSERTPC @pcnum int, @pcname varchar(50), @pctype int, @ipaddr ...
- Sublime Text3 protobuf syntax file(语法文件)
将以下两个文件放置在X:XXX\Sublime Text 3x64\Data\Packages\User目录下,就可以为sublime3添加protobuf文件的语法高亮规则. 文件名:Protobu ...
- Linux centos关机与重启命令详解与实战
Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.s ...
- Combine String---hdu5727 &&& Zipper(LCS变形)
题目链接:http://poj.org/problem?id=2192 http://acm.split.hdu.edu.cn/showproblem.php?pid=5707 http://acm. ...
- Face The Right Way---hdu3276(开关问题)
题目链接:http://poj.org/problem?id=3276 题意:n牛头排成一排,每头牛两个状态,向前或向后,为了让所有的牛都向前,现在有一个机器 每次 能控制连续K头牛转换自己的状态,求 ...
- [RVM is not a function] Interating RVM with gnome-terminal
Ubuntu 12.04 64bit LTS, running the 'rvm use 1.9.3' brings the 'RVM is not a function' warning. Here ...
- mysql tinyint smallint mediumint int bigint
类型 存储所占空间 (无论显示多少位) (单位 字节/bytes) 存储数据范围 最大显示长度 tinyint 1 -128 ~ 127 signed 0 ...
- js保留n位小数
1.功能:将浮点数四舍五入,取小数点后2位 function toDecimal(num) { var f = parseFloat(num); if (isNaN(f)) { return; } f ...
- 【转】关于Android资源文件中出现百分号的问题
关于Android资源文件中出现百分号的问题 分类: Android JAVA2014-08-01 16:53 1345人阅读 评论(0) 收藏 举报 ANDROID格式化资源文件 目录(?)[+ ...