HDOJ 4276 The Ghost Blows Light(树形DP)
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.
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)
1 2 2
2 3 2
2 5 3
3 4 3
1 2 3 4 5
思路
1. 经典树形 DP 题目
2. 动规数组定义: dp[i][j] 表示从节点 i 出发使用最大花费为 j 后重新回到节点 i 能够得到的最大价值
3. 状态转移方程: dp[i][j] = dp[c1][k1] + dp[c2][k2] + ... + dp[cn][kn], c1, c2 ... cn 为节点 i 的孩子, k1, k2 ... kn 是在其对应的孩子节点分配的时间,
k1+k2+...kn <= j
4. 盗贼必须要跑出去, 因此最短路径上的时间需要预支. 以最短路径上的点(u)为树根求出以 u 为根的树分配 j 时间能够得到的最大收益 dp[u][j]
5. 然后再计算, 如何在最短路径上的节点分配时间使总收益最大, 这也是一步 DP, 具体来说, 这依然是一步树形 DP
6. 实现分为 3 部分, 第一部分使用 BFS(bellmanford, dijkstra) 算出最短路径的耗费以及最短路径上的所有点; 第二部分是 (4); 第三部分是 (5)
代码 from kedebug
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define max(a,b) (((a) > (b)) ? (a) : (b))
const int INF = 1e9; struct Node {
int y, w;
Node(int _y, int _w) : y(_y), w(_w) { }
}; vector<Node> map[];
int dp[][]; // dp[i][j] 从i出发又返回i,最大花费为j时所取得的价值
int val[], mark[]; int bfs(int s, int e)
{
int dist[], f[];
for (int i = ; i < ; ++i)
dist[i] = INF;
f[s] = -;
dist[s] = ;
queue<int> q;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = ; i < map[u].size(); ++i)
{
Node &v = map[u][i];
if (dist[v.y] > dist[u] + v.w)
{
f[v.y] = u;
dist[v.y] = dist[u] + v.w;
q.push(v.y);
}
}
}
for (int i = e; i != -; i = f[i])
mark[i] = ;
return dist[e];
} void dfs(int u, int pre, int mval)
{
dp[u][] = val[u];
for (int i = ; i < map[u].size(); ++i)
{
int y = map[u][i].y;
int w = map[u][i].w;
if (y == pre || mark[y])
continue;
dfs(y, u, mval); for (int j = mval; j >= ; --j)
for (int k = ; k <= j-*w; ++k)
if (dp[u][j-k-*w] != - && dp[y][k] != -)
dp[u][j] = max(dp[u][j], dp[y][k] + dp[u][j-k-*w]);
}
} int main()
{
int n, t;
while (scanf("%d %d", &n, &t) == )
{
memset(map, , sizeof(map));
memset(dp, -, sizeof(dp));
memset(mark, , sizeof(mark)); int a, b, c;
for (int i = ; i < n; ++i)
{
scanf("%d %d %d", &a, &b, &c);
map[a].push_back(Node(b, c));
map[b].push_back(Node(a, c));
}
for (int i = ; i <= n; ++i)
scanf("%d", &val[i]); int tt = bfs(, n);
if (tt > t)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
for (int i = ; i <= n; ++i)
if (mark[i])
dfs(i, -, t - tt); int dp2[], tmax = t - tt;
memset(dp2, -, sizeof(dp2));
dp2[] = ;
for (int i = ; i <= n; ++i)
{
if (!mark[i])
continue;
for (int j = tmax; j >= ; --j)
{
for (int k = ; k <= j; ++k)
if (dp2[j-k] != - && dp[i][k] != -)
dp2[j] = max(dp2[j], dp2[j-k] + dp[i][k]); }
}
int ans = ;
for (int i = ; i <= tmax; ++i)
if (ans < dp2[i])
ans = dp2[i];
printf("%d\n", ans);
}
return ;
}
HDOJ 4276 The Ghost Blows Light(树形DP)的更多相关文章
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
- HDU4276 - The Ghost Blows Light(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
- 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,而且有可 ...
- 【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 ...
- BNUOJ 26283 The Ghost Blows Light
The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...
- 【HDU4276】The Ghost Blows Light
题目大意:给定一棵有根树,1 号节点为根节点,点有点权,边有边权,初始给定一个价值,每经过一条边都会减少该价值,每经过一个点都会增加相应的答案贡献值,求如何在给定价值的情况下最大化答案贡献,并要求最后 ...
- HDU-4276 The Ghost Blows Light (树形DP+背包)
题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...
随机推荐
- jquery 中 $ 符的意义
$()就是一个函数名,别把它想得这么神奇.$就是一个字符,比如function a(){};function $(){};是一样的.只是jquery中习惯用$函数来作为对象化的入口而已.你也可以把jq ...
- Spring MVC异常处理SimpleMappingExceptionResolver
Spring MVC异常处理SimpleMappingExceptionResolver[转] (2012-12-07 13:45:33) 转载▼ 标签: 杂谈 分类: 技术分享 Spring3.0中 ...
- 7、Qt MetaObject System详解
网上的资源比较乱,该文章整理自地址:http://www.xuebuyuan.com/735789.html Qt meta-object系统基于三个方面: 1.QObject提供一个基类,方便派生类 ...
- Unity入门教程(上)
一.介绍 目的:通过尝试制作一款使用玩家角色把小球弹飞的简单小游戏,熟悉使用Unity进行游戏开发的基本流程. 软件环境:Unity 2017.3.0f3,Visual Studio 2013 二.创 ...
- php后台管理员权限相关表结构
admin管理员表 id int(11) 用户id username varchar(128) 用户名 password varchar(128) 管理员密码 name varchar(50) 管理员 ...
- win10 .net framework 3.5无法安装错误代码0x800F081F
复制链接:http://download.windowsupdate.com/d/msdownload/update/software/updt/2015/11/microsoft-windows-n ...
- Unity3d游戏开发中使用可空类型(Nullable Types)
你怎么确定一个Vector3,int,或float变量是否被分配了一个值?一个方便的方式就是使用可空类型! 有时变量携带重要信息,但仅仅有在特定的游戏事件发生时触发.比如:一个角色在你的游戏可能闲置, ...
- VOIP NAT穿越之SIP信令穿越
本文原创自 http://blog.csdn.net/voipmaker 转载注明出处. 本文是VOIP通信NAT系列专题的第三篇, 本文论述NAT对SIP协议穿越的影响.SIP协议是基于文本的,而 ...
- 【玩转Golang】 自定义json序列化对象时,非法字符错误原因
由于前台web页面传来的日期对象是这样的格式“2010-11-03 15:23:22”,所以我安装网上查来的办法,自定义包装了time.Time对象,实现自己的Marshal和UnMarshal方法 ...
- python05 - 迭代器,生成器,装饰器
迭代器 迭代器就是访问集合元素的一种方式,迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问一遍后结束. 迭代器很大的特点: 只能往前迭代,不能够回退,也不能随机访问其中一个元素,只能通过__ ...