DijkstraAlgorithmTemplate question, Xi, the first most short-circuit, template question, originally fantasized about 1A, but unfortunately, one condition is missing, a very important condition, since it is the shortest short circuit, if there is a duplicate edge, of course, it is to save a shorter edge. As a result, I did not notice that it directly covered
Tragedy ,........................
After reading the four Shortest Path Algorithms for almost a day, I finally got a little bit of experience. Thanks to the detailed explanation on Daniel's blog
Http://www.wutianqi.com /? P = 1890 Dijkstra Algorithm Implementation
Http://www.wutianqi.com /? P = 1912 Bellman-Ford Algorithm
Http://www.wutianqi.com /? P = 2285Spfa (Shortest Path faster
Algorithm) Optimization of the previous Algorithm
// The above three are in-depth, with the single-source shortest path
Http://www.wutianqi.com /? P = 1903
// This is the requestReturns the shortest distance between any two points.
# Include <iostream> # include <string> using namespace STD; bool s [200]; int C [200] [200], DIS [200], n, m, S, t; void Dijkstra () {for (INT I = 0; I <n; I ++) {dis [I] = C [s] [I];} dis [s] = 0; For (INT I = 1; I <n; I ++) {int TMP = int_max, V = s; For (Int J = 0; j <n; j ++) if (! S [J] & dis [J] <TMP) {TMP = dis [J]; V = J;} s [v] = 1; for (Int J = 0; j <n; j ++) if (! S [J] & C [v] [J] <int_max) {int newdis = dis [v] + C [v] [J]; if (newdis <dis [J]) dis [J] = newdis ;}}int main () {int Len; while (CIN >>n >> m) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) c [I] [J] = int_max; int A, B; For (int K = 0; k <m; k ++) {CIN> A> B> Len; If (LEN <C [a] [B]) {C [a] [B] = Len; c [B] [a] = Len ;}} CIN >>> t; memset (S, 0, sizeof (s); s [s] = 1; dijkstra (); If (DIS [T] <int_max) cout <dis [T] <Endl; else cout <-1 <Endl;} return 0 ;}