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的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...
随机推荐
- HTML文件中使用Java程序
HTML文件中使用Java程序:简而言之,在HTML文件中引入java应用程序,并通过javascript调用其方法. 一. 运行环境 1.JAVA_HOME.CLASSPATH.PATH配置正确 ...
- Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 8款实用的Jquery瀑布流插件
1.网友Null分享Jquery响应式瀑布流布局插件 首先非常感谢网友Null的无私分享,此作品是一款响应式瀑布流布局Jquery插件,网友Null增加了一个屏幕自适应和响应式,响应式就是支持智能手机 ...
- LINQ to XML(1)
LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...
- php判断是否为手机客户端
function isWap() { $http_agent = isset ( $_SERVER ["HTTP_USER_AGENT"] ) ? $_SERVER ...
- WordPress 主题开发 - (三) 开发工具 待翻译
Before we get started building any WordPress Theme, we’re going to need to get our development tools ...
- Android API Level与sdk版本对照表
API等级1: Android 1.0 API等级2: Android 1.1 Petit Four 花式小蛋糕 API等级3: Android 1.5 Cupcake 纸杯蛋糕 API等级4: An ...
- referenceerror wx is not defined 微信JsSdk开发
如果你和我一样遇到了“referenceerror wx is not defined”错误,很有可能是jweixin-1.0.0.js与你其它某js冲突. 解决办法: <script type ...
- vs2010中的反编译
有这样的需求,一个.dll文件,如何查看里面的代码呢?网上有很多关于反编译的运用的. http://blog.csdn.net/lyflcear/article/details/8102057 昨天( ...
- Error LNK2005 从敌人到朋友
本人在写学生信息管理系统时遇到一个很头疼的错误——error LNK2005重复定义错误,苦思冥想百度谷歌bing之后都没能解决问题,于一清早刹那间觉得知道问题出在哪儿了,于是乎起床.开机.修改代码一 ...