Idea: a type of deformation of the Dijkstra algorithm directly applies the template. For more information, see my other article! It should be noted that when there is a duplicate edge in this question, in the actual application (if it is the same good way), of course, it is the path with the smallest choice =
AC code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define INF 0x3f3f3f3int map [212] [212], DIS [212], visited [212]; int n, m; void Dijkstra (int x) {int I, P, J, min; for (I = 0; I <n; I ++) {dis [I] = map [x] [I]; visited [I] = 0;} dis [x] = 0; visited [x] = 1; for (I = 0; I <n; I ++) {min = inf; For (j = 0; j <n; j ++) {If (! Visited [J] & dis [J] <min) {P = J; min = dis [J] ;}} visited [p] = 1; for (j = 0; j <n; j ++) {If (! Visited [J] & dis [p] + map [p] [J] <dis [J]) {dis [J] = dis [p] + map [p] [J] ;}}} int main () {int I, j, a, B, T, s, E; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {map [I] [J] = inf ;}for (I = 1; I <= m; I ++) {scanf ("% d ", & A, & B, & T); If (T <map [a] [B]) map [a] [B] = map [B] [a] = T;} scanf ("% d", & S, & E); Dijkstra (s ); if (DIS [e] = inf) printf ("-1 \ n"); else printf ("% d \ n", DIS [e]);} return 0 ;}