Floyd minimum ring Template
Code
/* Floyd minimum ring, record path, time complexity O (N ^ 3) cannot process negative ring */# include <iostream> # include <cstring> using namespace STD; const int INF = 109, maxn = 252645135; int G [INF] [INF], DIS [INF] [INF], pre [INF] [INF]; int ans [INF]; // PR [I] [J] records the first vertex int n, m, TEM, Tol; int main () in the I-to-J Shortest Path () {While (CIN> N> m) {TEM = maxn; memset (G, 0xf, sizeof g); memset (DIS, 0xf, sizeof dis); memset (PRE, 0, sizeof pre); int x, y, z; For (INT I = 1; I <= m; I ++) {CIN> x> Y> Z; If (z <G [x] [Y]) dis [x] [Y] = dis [y] [x] = G [x] [Y] = G [y] [x] = z; pre [x] [Y] = Y, pre [y] [x] = x ;}for (int K = 1; k <= N; k ++) {for (INT I = 1; I <K; I ++) for (Int J = I + 1; j <K; j ++) {If (TEM> dis [I] [J] + G [I] [k] + G [k] [J]) {TEM = dis [I] [J] + G [I] [k] + G [k] [J]; int T = I; Tol = 0; while (T! = 0) {ans [++ tol] = T; t = pre [T] [J];} ans [++ tol] = K ;}} for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) if (I! = J & dis [I] [k] + dis [k] [J] <dis [I] [J]) {dis [I] [J] = dis [I] [k] + dis [k] [J]; pre [I] [J] = pre [I] [k] ;}} if (TEM = maxn) cout <"no solution. "; elsefor (INT I = 1; I <= tol; I ++) cout <ans [I] <''; cout <Endl;} return 0 ;}
Poj 1734. sightseeing trip solution report