Question: There are n vertices and m rows under the undirected edge. m entries have the right to expand the path learning function of the smallest ring in the undirected edge graph. It is easier to find the ring first and then relax the dfs.
// Search is really nice # include <cstdio> # include <cstring> # include <iostream> # define find_min (a, B) a <B? A: B # define N 150 # define inf 0x7ffffff using namespace std; inline int Min (int a, int B) {return a> B? B: a;} int map [N] [N], dis [N] [N], pre [N] [N], path [N], n; int main () {int I, j, k, m, u, v, d; int num; while (~ Scanf ("% d", & n, & m) {for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) map [I] [j] = inf, pre [I] [j] = I; while (m --) {scanf ("% d", & u, & v, & d ); map [u] [v] = map [v] [u] = Min (map [v] [u], d);} memcpy (dis, map, sizeof (map); int ans = inf; for (k = 1; k <= n; k ++) {for (I = 1; I <k; I ++) for (j = I + 1; j <k; j ++) {int len = dis [I] [j] + map [I] [k] + map [k] [j]; if (len <ans) {ans = len; num = 0; int now = j; while (now! = I) path [num ++] = now, now = pre [I] [now]; // pre [I] [j] indicates I-> pre [I] [j]-> j path [num ++] = I; path [num ++] = k ;}}for (I = 1; I <= n; I ++) // common relaxation k points for (j = 1; j <= n; j ++) if (dis [I] [j]> dis [I] [k] + dis [k] [j]) {dis [I] [j] = dis [I] [k] + dis [k] [j]; pre [I] [j] = pre [k] [j]; // This learner} if (ans = inf) {printf ("No solution. \ n "); continue;} for (I = 0; I <num-1; I ++) printf (" % d ", path [I]); printf ("% d \ n", path [I]);} return 0 ;}