Given the distance between some intersections,-1 indicates that it cannot be reached.
The fire occurrence point and nearby fire department location are given.
Arrange the time and path of the Fire Department.
Reverse graph creation, starting with a fire spfa.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <queue> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6using namespace STD; int N; struct lx {int V, t ;}; vector <lx> G [21]; bool vis [21]; int path [21]; int dis [21]; void spfa (INT start) {for (INT I = 1; I <= N; I ++) dis [I] = inf, vis [I] = 0, path [I] = 0; q Ueue <int> q; vis [start] = 1, DIS [start] = 0; q. Push (start); While (! Q. empty () {int u = Q. front (); q. pop (); vis [u] = 0; For (Int J = 0; j <G [u]. size (); j ++) {int v = G [u] [J]. v; int T = G [u] [J]. t; If (DIS [v]> dis [u] + T) {dis [v] = dis [u] + T; path [v] = u; // record path if (! Vis [v]) {vis [v] = 1; q. push (v) ;}}}} struct node {int org, time;} l [21]; bool CMP (node A, Node B) {return. time <B. time;} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {int t; scanf ("% d", & T); if (I = j | T =-1) continue; lx now; now. V = I, now. T = T; G [J]. push_back (now) ;}} int start, endcot = 0; int thend [21]; scanf ("% d", & START); getchar (); char STR [101]; gets (STR); int I = 0; int C = 0; while (STR [I] = '') I ++; for (I = 0; I <= strlen (STR); I ++) {While (STR [I]> = '0' & STR [I] <= '9') C = C * 10 + STR [I]-'0 ', I ++; If (C! = 0) thend [endcot ++] = C, C = 0;} spfa (start); for (I = 0; I <endcot; I ++) L [I]. org = thend [I], L [I]. time = dis [thend [I]; sort (L, L + endcot, CMP); puts ("orgdesttimepath"); for (I = 0; I <endcot; I ++) {int v = L [I]. org; printf ("% d", V, start, DIS [v]); int TMP = V; while (path [TMP]! = 0) printf ("% d", TMP), TMP = path [TMP]; printf ("% d \ n", start );}}}
Poj 1122 FDNY to the rescue!