题意:有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)的更多相关文章

  1. UVA10269 Adventure of Super Mario(Floyd+DP)

    UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...

  2. ZOJ 1232 Adventure of Super Mario (Floyd + DP)

    题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...

  3. UVA 10269 Adventure of Super Mario

    看了这里 http://blog.csdn.net/acm_cxlove/article/details/8679230的分析之后自己又按照自己的模板写了一遍,算是对spfa又加深了一步认识(以前真是 ...

  4. 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] ...

  5. ZOJ1232 Adventure of Super Mario spfa上的dp

    很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的. 题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边.求由a+b->1的 ...

  6. [题解]UVA10269 Adventure of Super Mario

    链接:http://vjudge.net/problem/viewProblem.action?id=24902 描述:由城镇.村子和双向边组成的图,从A+B走到1,要求最短路.有K次瞬移的机会,距离 ...

  7. UVA-10269 Adventure of Super Mario (dijkstra)

    题目大意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用, ...

  8. zoj1232Adventure of Super Mario(图上dp)

    题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...

  9. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

随机推荐

  1. Redis基本操作-string

    Redis的5种数据结构:string.list.hash.set和zset; Redis 所有的数据结构都是以唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应的 value 数 ...

  2. 数据恢复软件extundelete介绍

    linux下文件系统一般由文件名.Inode.Block三部分组成.当一个用户在Linux系统中试图访问一个文件时,系统会先根据文件名去查找它的inode,看该用户是否具有访问这个文件的权限.如果有, ...

  3. redis数据迁移

    redis的备份和还原,借助了第三方的工具---redis-dump,  redis中使用redis-dump导出.导入.还原数据实例 1.安装redis-dump # yum install rub ...

  4. 正则表达式(Swift)

    课题 使用正则表达式匹配字符串 使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89" 返回匹配结果:'" ...

  5. 【348】通过 Numpy 创建各式各样的矩阵

    参考:NumPy之array-一个程序媛的自我修养-51CTO博客 参考:numpy中数组和矩阵的区别 - jiangsujiangjiang的博客 - CSDN博客 一.使用系统方法 二.用指定的数 ...

  6. scala private 和 private[this] 的区别

    class PackageStudy { private var a = 0 private[this] var b = 1 // 只能在类内部使用,对象都不能直接使用 def getb(): Int ...

  7. XtraBackup 备份原理

    来着淘宝技术: http://mysql.taobao.org/monthly/2016/03/07/ https://github.com/alibaba/AliSQL 前言 Percona Xtr ...

  8. C语言实现大数四则运算

    一.简介 众所周知,C语言中INT类型是有限制,不能进行超过其范围的运算,而如果采用float类型进行运算,由于float在内存中特殊的存储形式,又失去了计算的进度.要解决整个问题,一种解决方法是通过 ...

  9. 好用的模板引擎NVelocity

    CastleNVelocity-1.1.1,使用方法: 把dll放到项目中,添加引用,修改配置的文件夹以及数据模型,最后在逻辑代码中调用即可. 封装到CommonHelper.cs using Sys ...

  10. 使用Navicat Premium连接mysql数据库

    Navicat Premium是一个可多重连接的数据库管理工 具,它可让你以单一程序同时连接. Navicat Premium 使你能简单并快速地在各种数据库系统间传输数据,或传输一份指定 SQL 格 ...