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

析:先预处理出来使用工具能到达的距离,这个可以用Floyd 来解决,然后f[i][u] 表示到达 u 还剩下 i 次工具未用,然后用bfs就可以很简单的解决。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #define debug() puts("++++");
  21. #define gcd(a, b) __gcd(a, b)
  22. #define lson l,m,rt<<1
  23. #define rson m+1,r,rt<<1|1
  24. #define fi first
  25. #define se second
  26. #define pb push_back
  27. #define sqr(x) ((x)*(x))
  28. #define ms(a,b) memset(a, b, sizeof a)
  29. #define sz size()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define all 1,n,1
  34. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  35. #define freopenr freopen("in.txt", "r", stdin)
  36. #define freopenw freopen("out.txt", "w", stdout)
  37. using namespace std;
  38.  
  39. typedef long long LL;
  40. typedef unsigned long long ULL;
  41. typedef pair<int, int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const double inf = 1e20;
  44. const double PI = acos(-1.0);
  45. const double eps = 1e-8;
  46. const int maxn = 100 + 10;
  47. const int maxm = 1e5 + 10;
  48. const int mod = 50007;
  49. const int dr[] = {-1, 0, 1, 0};
  50. const int dc[] = {0, -1, 0, 1};
  51. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  52. int n, m;
  53. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  54. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  55. inline bool is_in(int r, int c) {
  56. return r >= 0 && r < n && c >= 0 && c < m;
  57. }
  58.  
  59. int dp[maxn][maxn];
  60. int f[12][maxn];
  61.  
  62. int main(){
  63. int T; cin >> T;
  64. while(T--){
  65. int A, B, L, K;
  66. scanf("%d %d %d %d %d", &A, &B, &m, &L, &K);
  67. ms(dp, INF);
  68. while(m--){
  69. int u, v, c; scanf("%d %d %d", &u, &v, &c);
  70. dp[u][v] = dp[v][u] = min(c, dp[u][v]);
  71. }
  72. FOR(k, 1, A+1) FOR(i, 1, A+B+1) FOR(j, 1, A+B+1)
  73. dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  74. ms(f, INF); f[K][A+B] = 0;
  75. queue<P> q; q.push(P(K, A+B));
  76. while(!q.empty()){
  77. P p = q.front(); q.pop();
  78. int u = p.se, i = p.fi;
  79. for(int v = 1; v < A+B; ++v){
  80. if(dp[u][v] == INF || u == v) continue;
  81. if(f[i][v] > f[i][u] + dp[u][v]){
  82. f[i][v] = f[i][u] + dp[u][v];
  83. q.push(P(i, v));
  84. }
  85. if(i && f[i-1][v] > f[i][u] && L >= dp[u][v]){
  86. f[i-1][v] = f[i][u];
  87. q.push(P(i-1, v));
  88. }
  89. }
  90. }
  91. int ans = INF;
  92. for(int i = 0; i <= K; ++i) ans = min(ans, f[i][1]);
  93. printf("%d\n", ans);
  94. }
  95. return 0;
  96. }

  

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. vue深入了解组件——处理边界情况

    一.访问元素&组件 在绝大多数情况下,我们最好不要触达另一个组件实例内部或手动操作DOM元素.不过也确实在一些情况下做这些事情是合适的. 1.1 访问根实例 在每个 new Vue 实例的子组 ...

  2. CentOS开机卡在进度条,无法正常开机的排查办法

    CentOS开机的时候卡在进度条一直进不去 重启,按f5键进度条/命令行界面方式切换,确认卡问题后处理就好 我这边卡在redis服务,设置为开机启动但是一直服务启动不起来 重启按住"e&qu ...

  3. oracle老是出现监听错误

    解决方法之一: 点击开始-->选择程序--->选中你安装的oracle版本-->选中配置与移植工具-->选中Net Configuration Assistant-->在 ...

  4. Winform 无纸化办公-屏幕联动

    最近做无纸化办公,对接硬件,用了挺多东西总结一下 技术上主要是:asp.net .winform.activeX控件.chrome插件.socket编程,websocket. 其实看着需求挺简单的,在 ...

  5. ingress 密码验证

    traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...

  6. Java的Reflection机制

    什么时候使用Reflection: 在java语言中,创建一个类的对象通常使用new operator,但是如果预先不知道Class的名字,类名是在程序运行过程中通过参数传递过来,就没法使用这种方法了 ...

  7. sibling

    sibling 英 ['sɪblɪŋ] 美 ['sɪblɪŋ] 名词. 兄,弟,姐,妹网络. 兄弟,兄弟姐妹,同胞变形. 复数:siblings

  8. Hammer.js——给bootstrap添加触屏功能

    Hammer.js qq群号(html5技术交流):158677025   手机端演示二维码(或直接在手机中输入网址:http://lilinfeng.cncoder.me 浏览效果): 一.前言 移 ...

  9. 指针c艹

    #include <iostream> using namespace std;int value=1;void func(int *p){ p=&value; }void fun ...

  10. python的进程间的数据交互

    #先来看下如何实现多进程 # multiprocessing 这个是python的多进程的模块,我们会用到这个模块的很多方法 from multiprocessing import Process i ...