Topic Links: poj1122 FDNY to the rescue!
Test instructions: Given the matrix, each element of the matrix Tij represents the time required from the intersection to the J intersection, if the Tij is 1 means that there is no direct path between the two intersections, and then gives the intersection where the fire alarm is located and the intersection location of one or more fire stations. Output requirements from fire station to fire location of the time required to arrange from small to large, output information including fire station location (initial position), fire location (target location), the time required, the shortest path at each intersection.
The inverse of the map, from the location of the fire alarm to find the shortest time, to find a minimum road record path, according to the time from small to large output.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineCLR (A, B) memset ((a), (b), sizeof ((a)))5 using namespacestd;6 7 Const intINF =0x3f3f3f3f;8 Const intN = +;9 intN, fire;Ten intG[n][n];//adjacency matrix storage diagram One intS[n], d[n]; A intPath[n];//represents the previous vertex ordinal of the v0 to VI shortest path vertex VI - intShortest[n];//Store each vertex ordinal on the shortest path - structnode{ the intu, V, t; - }a[n]; - intCMP (Node A, Node B) { - returnA.T <b.t; + } - voidDij () { + intI, J, K; ACLR (S,0); at for(i =1; I <= N; ++i) { -D[i] =G[fire][i]; - if(I! =Fire ) -Path[i] =Fire ; - Else -Path[i] =-1; in } -S[fire] =1; toD[fire] =0; + for(i =0; I < n1; ++i) { - intMI = inf, u =0; the for(j =1; J <= N; ++j) { * if(!s[j] && D[j] <mi) { $U = j; Mi =D[j];Panax Notoginseng } - } theS[u] =1; + for(k =1; K <= N; ++k) { A if(!s[k] && D[u] + g[u][k] <D[k]) { theD[K] = D[u] +G[u][k]; +PATH[K] =u; - } $ } $ } - } - intMain () { the intI, J, X, CNT; -scanf"%d", &n);Wuyi for(i =1; I <= N; ++i) {//Edge Reverse Storage the for(j =1; J <= N; ++j) { -scanf"%d", &x); Wu if(x = =-1) -G[j][i] =inf; About Else $G[j][i] =x; - } - } -scanf"%d", &Fire ); A Dij (); +CNT =0; the while(SCANF ("%d", &x) = =1){ -A[CNT].U =x; $A[CNT].V =Fire ; thea[cnt++].t =D[x]; the } theSort (A, A +CNT, CMP); theprintf"org\tdest\ttime\tpath\n"); - for(i =0; I < CNT; ++i) { inprintf"%d\t%d\t%d", A[i].u, A[I].V, a[i].t); theCLR (Shortest,0); the intK =0;//Represents the subscript of the last element in the shortest array AboutSHORTEST[K] =a[i].u; the while(Path[shortest[k]]! =-1){ thek++; theShortest[k] = path[shortest[k-1]]; + } - for(j =0; J <= K; ++J)//Backward tracking, but inverse build, so forward output theprintf"\t%d", Shortest[j]);BayiPuts""); the } the return 0; -}
View Code
poj1122 FDNY to the rescue! (dij+ reverse build + output path)