UVA 10269 Adventure of Super Mario (Floyd + Dijkstra)

Source: Internet
Author: User
Tags cos

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.