UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)
题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径。但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄。这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在村庄内部可使用。另外,在城市或村庄内部的时间不计。
析:先预处理出来使用工具能到达的距离,这个可以用Floyd 来解决,然后f[i][u] 表示到达 u 还剩下 i 次工具未用,然后用bfs就可以很简单的解决。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[maxn][maxn];
int f[12][maxn]; int main(){
int T; cin >> T;
while(T--){
int A, B, L, K;
scanf("%d %d %d %d %d", &A, &B, &m, &L, &K);
ms(dp, INF);
while(m--){
int u, v, c; scanf("%d %d %d", &u, &v, &c);
dp[u][v] = dp[v][u] = min(c, dp[u][v]);
}
FOR(k, 1, A+1) FOR(i, 1, A+B+1) FOR(j, 1, A+B+1)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
ms(f, INF); f[K][A+B] = 0;
queue<P> q; q.push(P(K, A+B));
while(!q.empty()){
P p = q.front(); q.pop();
int u = p.se, i = p.fi;
for(int v = 1; v < A+B; ++v){
if(dp[u][v] == INF || u == v) continue;
if(f[i][v] > f[i][u] + dp[u][v]){
f[i][v] = f[i][u] + dp[u][v];
q.push(P(i, v));
}
if(i && f[i-1][v] > f[i][u] && L >= dp[u][v]){
f[i-1][v] = f[i][u];
q.push(P(i-1, v));
}
}
}
int ans = INF;
for(int i = 0; i <= K; ++i) ans = min(ans, f[i][1]);
printf("%d\n", ans);
}
return 0;
}
UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)的更多相关文章
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- ZOJ 1232 Adventure of Super Mario (Floyd + DP)
题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...
- UVA 10269 Adventure of Super Mario
看了这里 http://blog.csdn.net/acm_cxlove/article/details/8679230的分析之后自己又按照自己的模板写了一遍,算是对spfa又加深了一步认识(以前真是 ...
- 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] ...
- ZOJ1232 Adventure of Super Mario spfa上的dp
很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的. 题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边.求由a+b->1的 ...
- [题解]UVA10269 Adventure of Super Mario
链接:http://vjudge.net/problem/viewProblem.action?id=24902 描述:由城镇.村子和双向边组成的图,从A+B走到1,要求最短路.有K次瞬移的机会,距离 ...
- UVA-10269 Adventure of Super Mario (dijkstra)
题目大意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用, ...
- zoj1232Adventure of Super Mario(图上dp)
题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...
随机推荐
- Java对字符串使用MD5进行加密(亲测有效)
转自:https://blog.csdn.net/jay314159/article/details/4918358 前言: MD5即Message-Digest Algorithm 5(信息-摘要算 ...
- dapper.net 转载
Dapper.NET——轻量ORM Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2 ...
- Python线程优先级队列(Queue)
Python的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列 LifoQueue,和优先级队列PriorityQueue.这些队列都实 ...
- Hibernate 的update语句性能详解
Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to clipboardprint? p ...
- "\\s+"的使用
详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页符等等, 等价于[ \f\n\r\t\v] \f -> 匹配一个换页 \n -> 匹配一个换 ...
- Flexvolume
https://kubernetes.io/docs/concepts/storage/volumes/ https://github.com/kubernetes/community/blob/ma ...
- Containerpilot 配置文件模板
{ "consul": "{{ .CONSUL }}:8500", "logging": { "level": &quo ...
- Java工具类_模拟HTTP POST请求
import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.InputStream; i ...
- Java开发微信公众号
1.https://natapp.cn/ 2.选择和自己电脑相对应的系统下载 (以windows为例)下载并解压在磁盘中(记住解压文件的位置 例如:E:\dailysoftware\ngrok_wi ...
- sql批量修改字段内容的语句-SQL技巧
--update '表名' set 要修改字段名 = replace (要修改字段名,'被替换的特定字符','替换成的字符')--update tRecord set columnName = rep ...