HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739
做这道题主要是为了加强自己对SPFA的代码的训练以及对树dp的一些思路的锻炼。我特地研究了一下树dp的部分
for (int i = t; i >= w; i--){
for (int j = i-w; j >= 0; j--){
dp[u][i] = max(dp[u][i], dp[u][j]+dp[v][i - j - w]);
}
}
循环里面是不能搞错顺序的,外层的i逆序显然,但为什么里层会有问题呢? 因为w是可以=0的,这个时候想像一下,如果j=i-w的话,那么就会有
dp[u][i]=max(dp[u][i],dp[u][i]+dp[v][i-j-w]),但是dp[u][i]是已经更新了的,所以这样会出错,两层循环都必须要逆序。
下面贴一记代码
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define maxn 150
using namespace std; struct Edge
{
int u, v, w;
Edge(){}
Edge(int ui, int vi, int wi) :u(ui), v(vi), w(wi){}
}e[2*maxn]; int n, t;
int val[maxn];
int ecnt; int first[maxn], nxt[2 * maxn]; void add(int u, int v, int w)
{
e[ecnt].u = u; e[ecnt].v = v; e[ecnt].w = w;
nxt[ecnt] = first[u];
first[u] = ecnt++;
} int dis[maxn], vis[maxn];
int p[maxn]; void spfa(int s)
{
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(p, -1, sizeof(p));
queue<int> que;
que.push(s); vis[s] = 1;
dis[s] = 0;
while (!que.empty())
{
int u = que.front(); que.pop();
vis[s] = 0;
for (int i = first[u]; i != -1; i = nxt[i]){
int v = e[i].v;
if (dis[v] > dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
p[v] = i;
if (!vis[v]){
que.push(v);
vis[v] = 1;
}
}
}
}
} int dp[maxn][550]; void dfs(int u, int fa)
{
for (int i = first[u]; i != -1; i = nxt[i]){
int v = e[i].v, w = e[i].w * 2;
if (v == fa) continue;
dfs(v, u);
for (int i = t; i >= w; i--){
for (int j = i-w; j >= 0; j--){
dp[u][i] = max(dp[u][i], dp[u][j]+dp[v][i - j - w]);
}
}
}
for (int i = 0; i <= t; i++){
dp[u][i] += val[u];
}
} int main()
{
while (cin >> n >> t)
{
int ui, vi, wi;
memset(first, -1, sizeof(first));
ecnt = 0;
for (int i = 0; i < n - 1; i++){
scanf("%d%d%d", &ui, &vi, &wi);
add(ui, vi, wi);
add(vi, ui, wi);
}
for (int i = 1; i <= n; i++) scanf("%d", val + i);
spfa(1);
if (dis[n]>t) {
puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!");
continue;
}
for (int i = n; i != 1; i = e[p[i]].u){
e[p[i]].w = e[p[i] ^ 1].w = 0;
}
t -= dis[n];
memset(dp, 0, sizeof(dp));
dfs(1, -1);
printf("%d\n", dp[1][t]);
}
return 0;
}
HDU4276 The Ghost Blows Light SPFA&&树dp的更多相关文章
- HDU4276 The Ghost Blows Light(树形DP+背包)
题目大概说一棵n个结点树,每个结点都有宝藏,走过每条边要花一定的时间,现在要在t时间内从结点1出发走到结点n,问能获得最多的宝藏是多少. 放了几天的题,今天拿出来集中精力去想,还是想出来了. 首先,树 ...
- HDU-4276 The Ghost Blows Light (树形DP+背包)
题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...
- 【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 ...
- HDU4276 - The Ghost Blows Light(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
- HDU 4276-The Ghost Blows Light(树状背包)
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
- HDU 4276 The Ghost Blows Light (树形DP,变形)
题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...
- 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 ...
- 树形DP(01组合背包The Ghost Blows Light HDU4276)
题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...
随机推荐
- JAVA作业-1
编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果. import javax.swing.JOptionPane; // import class JOptionPane publ ...
- Linux命令行提示符设置
我们使用Linux系统时接触最多的是它的命令行窗口,很多时候我们都需要在命令行上输入命令,在输入的命令前都会有提示符,一般系统默认的提示符形式是:[username@host 工作目录]$. 其实,我 ...
- WordPress使用360CDN替换google服务,解决WordPress打开速度变慢问题
由于wordpress新版本中默认使用了一些google服务及google字体,由于google在天朝中的一些你懂的原因,造成wordpress在打开时被拖慢,这时可以用360推出的公共库CDN服务替 ...
- C# 整形数组排序
static void Main(string[] args) { , , , , , , , , , }; Array.Sort(numbers); Array.ForEach<int> ...
- linux 终端显示 -bash-4.1
解决方法: cp /etc/skel/.bashrc /root/ cp /etc/skel/.bash_profile /root/ 重新登陆就OK了
- jQuery取值相加
实在是太菜了. 这样一个需求: 计算两个text中的值的和,引发了对jQuery中类型转换的知识软肋. 在网上找到了才知道:http://zhidao.baidu.com/link?url=ujw88 ...
- 百度云demo2
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- Struts 2简单配置分析
要配置Struts 2,首先先要有Struts 2的Jar包,可以去Struts的官网下载(http://struts.apache.org/),这里有3个GA版本可以选择下载,我选择的是最新的2.2 ...
- .NET4.5中WCF中默认生成的basicHttpsBinding的研究
起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHt ...