ZOJ1232 Adventure of Super Mario spfa上的dp
很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的。
题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边。求由a+b->1的最短路,但是你有很多飞鞋,每双飞鞋可以跑一个固定的距离l,但是跑的时候要是碰到了城堡就要停下来,而且也不能停在路中间。
思路和代码参考了下面的这个网址:http://blog.csdn.net/acm_cxlove/article/details/8679230
思路:正常来说我们就跑SPFA就可以了,每次从队列里取点更新,但因为有了跑鞋,所以每次取点的时候还有多一种转移途径,因此我们就需要知道每次用了跑鞋之后从当前的点能够到达哪些点,这个时候我们就需要两点之间的距离,因为两点之间不能经过城堡,所以在跑SPFA的时候我们对于那些城堡的点我们就不再压入的跑SPFA.然后当dis[u][v]<=l的时候我们就可以转移了。
#pragma warning(disable:4996)
#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define maxn 120
#define maxm 120*120
#define inf 0x3f3f3f3f
using namespace std; int a, b, m, l, k; struct Edge
{
int u, v, w;
Edge(){}
Edge(int ui, int vi, int wi) :u(ui), v(vi), w(wi){}
}e[maxm];
int ecnt; int dis[maxn][maxn];
int vis[maxn];
int first[maxn];
int nxt[maxm]; 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++;
} void spfa()
{
memset(dis, 0x3f, sizeof(dis));
queue<int> que;
for (int loc = 1; loc <= a + b; loc++){
while (!que.empty()) que.pop();
memset(vis, 0, sizeof(vis));
que.push(loc); vis[loc] = 1;
dis[loc][loc] = 0;
while (!que.empty()){
int u = que.front(); que.pop(); vis[u] = 0;
for (int i = first[u]; i != -1; i = nxt[i]){
int v = e[i].v, w = e[i].w;
if (dis[loc][v] > dis[loc][u] + w){
dis[loc][v] = dis[loc][u] + w;
if (v <= a&&!vis[v]){
que.push(v); vis[v] = 1;
}
}
}
}
}
} int dp[maxn][15]; int main()
{
int T; cin >> T;
while (T--)
{
scanf("%d%d%d%d%d", &a, &b, &m, &l, &k);
memset(first, -1, sizeof(first)); ecnt = 0;
int ui, vi, wi;
for (int i = 0; i < m; i++){
scanf("%d%d%d", &ui, &vi, &wi);
add(ui, vi, wi);
add(vi, ui, wi);
}
spfa();
memset(dp, 0x3f, sizeof(dp));
memset(vis, 0, sizeof(vis));
queue<int> que;
que.push(a + b);
dp[a + b][k] = 0;
vis[a + b] = 1;
while (!que.empty()){
int u = que.front(); que.pop(); vis[u] = 0;
for (int sh = 0; sh <= k; sh++){
for (int i = first[u]; i != -1; i = nxt[i]){
int v = e[i].v, w = e[i].w;
if (dp[v][sh] > dp[u][sh] + w){
dp[v][sh] = dp[u][sh] + w;
if (!vis[v]){
que.push(v); vis[v] = 1;
}
}
}
if (!sh) continue;
for (int v = 1; v <= a + b; v++){
if (u != v&&dis[u][v] <= l){
if (dp[v][sh - 1] > dp[u][sh]){
dp[v][sh - 1] = dp[u][sh];
if (!vis[v]){
que.push(v); vis[v] = 1;
}
}
}
}
}
}
int ans = inf;
for (int i = 0; i <= k; i++){
ans = min(ans, dp[1][i]);
}
printf("%d\n", ans);
}
return 0;
}
---恢复内容结束---
ZOJ1232 Adventure of Super Mario spfa上的dp的更多相关文章
- ZOJ1232 Adventure of Super Mario(DP+SPFA)
dp[u][t]表示从起点出发,到达i点且用了t次magic boot时的最短时间, 方程如下: dp[v][t]=min(dp[v][t],dp[u][t]+dis[u][v]); dp[v][t] ...
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- [题解]UVA10269 Adventure of Super Mario
链接:http://vjudge.net/problem/viewProblem.action?id=24902 描述:由城镇.村子和双向边组成的图,从A+B走到1,要求最短路.有K次瞬移的机会,距离 ...
- UVA 10269 Adventure of Super Mario
看了这里 http://blog.csdn.net/acm_cxlove/article/details/8679230的分析之后自己又按照自己的模板写了一遍,算是对spfa又加深了一步认识(以前真是 ...
- UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)
题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在 ...
- UVA-10269 Adventure of Super Mario (dijkstra)
题目大意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用, ...
- ZOJ 1232 Adventure of Super Mario (Floyd + DP)
题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...
- ZOJ2923 Calculate Roads(SPFA上的dp)
算是学了图dp后的第一次应用吧.题目其实真的是非常不严谨,什么都没说,基本靠猜,而且严格来说数据应该会有爆int的,不过不管那么多啦,思路对了就好- -0 #include<iostream&g ...
- zoj1232Adventure of Super Mario(图上dp)
题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...
随机推荐
- GDAL读取tiff文件/C++源码
// gdal_geotiff.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "gdal_priv.h&quo ...
- Regionals 2013 :: North America - Southeast USA
Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...
- 【风马一族_Java】java的一种IDE
IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7 ...
- 《Apache之访问本地用户家目录》——RHEL6.3
首先保证这个本地用户是系统上有的. 1.安装httpd软件包: Yum install httpd 2.启动apache服务: 3.配置用户的家目录: 4.打开apache访问家目录的权限: 5.配置 ...
- wap网站seo如何优化呢?
从事互联网的人员都知道移动互联网营销是一个大的趋势,但是要怎么去做恐怕还都一筹莫展.由PC端的网络营销的经验和常识来看,首要的是要做好移动端手机网站的优化工作.据观察分析,目前国内的大多数并没有做好手 ...
- HTML5-WebWorker
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Linux 下安装服务器安全狗
1.网上下载服务器安全狗的软件包 32位和64位 wget http://www.safedog.cn/server_safedog_linux.html/safedog_linux32.tar ...
- .Net Core下如何管理配置文件(转载)
原文地址:http://www.cnblogs.com/yaozhenfa/p/5408009.html 一.前言 根据该issues来看,System.Configuration在.net core ...
- malloc函数
C语言中,使用malloc函数向内存中动态申请空间. 函数的原型是extern void *malloc(unsigned int num_bytes); 可见,函数返回的是指针类型,参数是要申请的空 ...
- .NET开发作业调度(job scheduling) - Quartz.NET
Quartz.NET是JAVA Job Scheduling框架Quartz在.NET平台上的实现,可以满足小型乃至大型企业应用中的Job Scheduling. 通过Nuget安装Quartz.NE ...