树形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. 题目描述中有两句话, ...
随机推荐
- href="#"和javasrcript:void(0)的区别
当我们需要一个空链接时,通常有两种方法: <a href="#">这个一个空链接</a> 和 <a href="javascript:voi ...
- [LeetCode] Simplify Path(可以不用看)
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- 使用Areas分离ASP.NET MVC项目
为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...
- Wall---hdu1348(求凸包周长 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...
- CSS布局属性
一.弹性盒模型介绍 1.弹性盒模型介绍 — 基础知识 弹性盒模型( Flexible Box 或 Flexbox)是一个CSS3新增布局模块,官方称为CSS Flexible Box Layout M ...
- undefined和void
1.undefined undefined在js中并不是关键字/保留字,因此在IE5.5~8中可以对undefined赋值,但是在IE9以上,对其赋值是无效的 <script> var a ...
- Java学习-032-JavaWeb_001 -- Tomcat环境部署及基本配置
首先到 Tomcat 官网,下载对应的版本,我本机的系统是 WIN7 64BIT 的,因而我选择的是64bit 的zip包,如下图所示:
- HTML-003-模拟IDE代码展开收起功能简单示例
当先我们在日常的编程开发工作中使用编程工具(例如 Eclipse.Sublime 等等)都有相应的代码折叠展开功能,如下图所示,极大的方便了我们的编码工作.
- c#中的linq二
c#中的linq二 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- C#中数组、ArrayList和List三者的区别
在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...