UVA 10269 Adventure of Super Mario Title: There is a village, B Castle, village number from 1~a, Castle number from A + 1 ~ a + B. Mario lives in village 1th and the princess is held in A + B castle. Mario has a treasure that allows him to run the distance of L in an instant, but there is a limit to this treasure. The starting or ending point of launching this treasure must be a village or a castle, and cannot pass through the castle. Such treasures of course can not be used casually, so its durability is only k, that is, at most only k times, will get the blacksmith shop to repair. Now, Mario has saved the princess in A + B castle and asked Mario how much time it takes to return to village 1th. Problem-solving ideas: first with Floyd to deal with all the edges, optimize the edge of the optimization (the point for the castle can not be optimized, the K from 1 to a on the line, I and J or 1~a + B), for the back use of equipment skills to prepare. Then use Dijkstra to find the shortest time. The D[i][j] array, open to two-dimensional, I represents the shortest time from the starting point to the I point, J indicates that in the current situation using a few skills, so two judged
D[i][k] > D[u][k] + g[u][i]And
G[u][i] <= L && d[i][k-1] > d[u][k] && k! = 0。 Dijkstra optimize the priority queue again, when you reach village 1th for the first time, you get the answer.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN =155;Const intINF =0x3f3f3f3f;typedef Long LongllintA, B, M, L, K;intG[n][n];voidFloyd () { for(intK =1; K <= A; k++) { for(inti =1; I <= A + B; i++) { for(intj =1; J <= A + B; J + +) {if(G[i][j] > G[i][k] + g[k][j]) {G[i][j] = G[i][k] + g[k][j]; } } } }}structnode{intU, K,Cos;BOOL operator< (Constnode& a)Const{return Cos> A.Cos; }};intd[n][ the];intDijkstra () {priority_queue<node> Q; for(inti =0; I <= A + B; i++) { for(intj =0; J <= A; J + +) {D[i][j] = INF; }} q.push ((Node) {A + B, K,0}); D[a + b][k] =0; while(! Q.empty ()) {intU = q.top (). u;intK = Q.top (). k;if(U = =1)returnQ.top ().Cos; Q.pop (); for(inti =1; I <= A + B; i++) {if(i = = u)Continue;if(G[u][i] = = INF)Continue;if(D[i][k] > D[u][k] + g[u][i]) {D[i][k] = D[u][k] + g[u][i]; Q.push (Node) {i, K, D[i][k]}); }if(G[u][i] <= L && d[i][k-1] > D[u][k] && k! =0) {d[i][k-1] = D[u][k]; Q.push (Node) {i, K-1, D[i][k-1]}); } } }}voidInput () {scanf("%d %d%d%d%d", &a, &b, &m, &l, &k); for(inti =1; I <= A + B; i++) { for(intj =1; J <= A + B; J + +) {G[i][j] = INF; } }intA, B, C; for(inti =0; i < M; i++) {scanf(" %d%d%d", &a, &b, &c); G[A][B] = g[b][a] = C; } Floyd ();}intMain () {intTscanf("%d", &t); while(t--) {input ();printf("%d\n", Dijkstra ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.
UVA 10269 Adventure of Super Mario (Floyd + Dijkstra)