question: a cow returns home along the road sign and finds the shortest path.
problem solving: handle duplicate edges. It feels good. In the future, we will use the vector graph for undirected graphs, which can save the processing of the duplicate edge. Graph of the edge of a directed graph using the recorded header node.
# Include <cstdio> # include <queue> # include <cstring> using namespace STD; # define Max 10000 # define INF 9999999 struct edge {int V, W, next ;}; edge edge [Max]; int head [Max], E; int d [Max], inque [Max]; void spfa (int n) {int U, V, I; for (I = 1; I <= N; I ++) d [I] = inf, inque [I] = 0; d [N] = 0; queue <int> que; que. push (n); inque [N] = true; while (que. empty () = false) {u = que. front (); que. pop (); inque [U] = false; for (I = head [u]; I! =-1; I = edge [I]. next) {v = edge [I]. v; If (d [v]> d [u] + edge [I]. w) {d [v] = d [u] + edge [I]. w; If (inque [v]) continue; inque [v] = true; que. push (v) ;}}} void add (int u, int V, int W) {edge [e]. V = V; edge [e]. W = W; edge [e]. next = head [u]; head [u] = e ++;} int main () {int t, n, U, V, W; scanf ("% d", & T, & N); memset (Head,-1, sizeof (head); E = 0; while (t --) {scanf ("% d", & U, & V, & W); add (U, V, W); add (v, U, w);} spfa (n); printf ("% d \ n", d [1]); Return 0 ;}