Link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2544
Question:
Problem description in each year's competition, all the finalists will get a very beautiful T-shirt. However, every time our staff moved hundreds of pieces of clothing from the store back to the stadium, they were very tired! So now they want to find the shortest route from the store to the stadium. Can you help them?
The input includes multiple groups of data. The first row of each group of data is two integers, N and M (n <= 100, m <= 10000). N indicates several intersections on the streets of Chengdu, the intersection marked as 1 is the location of the store, the intersection marked as n is the location of the stadium, and m represents several roads in Chengdu. N = m = 0 indicates that the input is complete. In the next m row, each row contains three integers, A, B, and C (1 <= A, B <= N, 1 <= C <= 1000 ), it means there is a road between Intersection a and intersection B. Our staff need to walk this road in C minutes.
Enter a route to ensure there is at least one store.
Output outputs a line for each input group, indicating the shortest time for a staff member to walk from the store to the stadium.
Sample Input
2 11 2 33 31 2 52 3 53 1 20 0
Sample output
32
Sourceuestc 6th Programming Contest online
The minimum short circuit is used to verify various short circuit templates.
1. Dijkstra basic Edition
#include<cstdio>#include<cstring>const int N=105, INF=9999999;int d[N], w[N][N],vis[N],n,m;void Dijkstra(int src){ for(int i=1; i<=n; ++i) d[i] = INF; d[src] = 0; memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i){ int u=-1; for(int j=1; j<=n; ++j)if(!vis[j]){ if(u==-1 || d[j]<d[u]) u=j; } vis[u] = 1; for(int j=1; j<=n; ++j)if(!vis[j]){ int tmp = d[u] + w[u][j]; if(tmp<d[j]) d[j] = tmp; } }}int main(){ int a,b,c; while(~scanf("%d%d",&n,&m)&&n+m){ for(int i=1; i<=n; ++i){ w[i][i] = INF; for(int j=i+1; j<=n; ++j) w[i][j] = w[j][i] = INF; } for(int i=0; i<m; ++i){ scanf("%d%d%d",&a,&b,&c); w[a][b] = w[b][a] = c; } Dijkstra(1); printf("%d\n", d[n]); } return 0;}
2. Dijkstra + adjacent table (implemented by array) + priority queue Optimization
# Include <cstdio> # include <cstring> # include <utility> # include <queue> using namespace STD; const int n = 20005; const int INF = 9999999; typedef pair <int, int> PII; priority_queue <PII, vector <PII>, greater <PII> q; int d [N], first [N], U [N], V [N], W [N], next [N], n, m; bool vis [N]; // undirected graph input, note that each input edge should be considered as two sides void read_graph () {memset (first,-1, sizeof (first); // initialize the header for (int e = 1; E <= m; ++ e) {scanf ("% d", & U [e], & V [e], & W [e]); U [E + M] = V [E]; V [E + M] = U [E]; W [E + M] = W [E]; // Add a reverse edge to it. Next [e] = first [U [e]; // Insert the first [U [e] = E; next [E + M] = first [U [E + M]; // Insert the chain table first [U [E + M] = e + m;} void Dijkstra (int src) {memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; ++ I) d [I] = inf; d [SRC] = 0; q. push (make_pair (d [SRC], Src); While (! Q. empty () {PII u = Q. top (); q. pop (); int x = u. second; If (vis [x]) continue; vis [x] = true; For (int e = first [X]; e! =-1; E = next [e]) if (d [V [e]> d [x] + W [e]) {d [V [e] = d [x] + W [E]; q. push (make_pair (d [V [e], V [e]) ;}} int main () {int A, B, C; while (~ Scanf ("% d", & N, & M) & N + M) {read_graph (); Dijkstra (1); printf ("% d \ n ", d [N]);} return 0 ;}
3. Dijkstra + adjacent table (implemented using vecor) + priority queue Optimization
# Include <cstdio >#include <cstring >#include <utility >#include <queue >#include <vector> using namespace STD; const int n = 105; const int INF = 9999999; typedef pair <int, int> PII; vector <PII> G [N]; priority_queue <PII, vector <PII>, greater <PII> q; int d [N], first [N], U [N], V [N], W [N], next [N], n, m; bool vis [N]; // input of an undirected graph. Note that an edge that is not input must be considered as void read_graph () {for (INT I = 1; I <= N; ++ I) g [I]. clear (); int A, B, C; For (INT I = 1; I <= M; ++ I) {scanf ("% d", & A, & B, & C); G [A]. push_back (make_pair (B, c); G [B]. push_back (make_pair (a, c) ;}} void Dijkstra (int src) {memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; ++ I) d [I] = inf; d [SRC] = 0; q. push (make_pair (d [SRC], Src); While (! Q. empty () {pii t = Q. top (); q. pop (); int u = T. second; If (vis [u]) continue; vis [u] = true; For (int v = 0; v <G [u]. size (); ++ v) if (d [G [u] [v]. first]> d [u] + G [u] [v]. second) {d [G [u] [v]. first] = d [u] + G [u] [v]. second; q. push (make_pair (d [G [u] [v]. first], G [u] [v]. first) ;}}int main () {int A, B, C; while (~ Scanf ("% d", & N, & M) & N + M) {read_graph (); Dijkstra (1); printf ("% d \ n ", d [N]);} return 0 ;}
2. Bellman-Ford Algorithm
# Include <cstdio> # include <cstring> # include <utility> # include <queue> using namespace STD; const int n = 20005; const int INF = 9999999; int N, m, U [N], V [N], W [N], d [N]; // input of an undirected graph, note that each input edge is considered as two inline void read_graph () {for (int e = 1; E <= m; ++ E) {scanf ("% d", & U [e], & V [e], & W [e]) ;}} inline void bellman_ford (int src) {for (INT I = 1; I <= N; ++ I) d [I] = inf; d [SRC] = 0; For (int K = 0; k <n-1; ++ K) {for (INT I = 1; I <= m; ++ I ) {Int x = U [I], y = V [I]; If (d [x] <inf) {If (d [y]> d [x] + W [I]) d [y] = d [x] + W [I];} if (d [y] <inf) {If (d [x]> d [y] + W [I]) d [x] = d [y] + W [I] ;}}} int main () {int A, B, C; while (~ Scanf ("% d", & N, & M) & N + M) {read_graph (); bellman_ford (1); printf ("% d \ n ", d [N]);} return 0 ;}
3. spfa
Implement the adjacent table
#include<cstdio>#include<cstring>#include<utility>#include<queue>using namespace std;const int N=20005;const int INF=2147483646>>1;int n, m, first[N],next[N],u[N],v[N],w[N], d[N];bool vis[N];queue<int>q;inline void read_graph(){ memset(first, -1, sizeof(first)); for(int e=1; e<=m; ++e){ scanf("%d%d%d",&u[e],&v[e],&w[e]); u[e+m]=v[e], v[e+m]=u[e], w[e+m]=w[e]; next[e] = first[u[e]]; first[u[e]] = e; next[e+m] = first[u[e+m]]; first[u[e+m]] = e+m; }}void SPFA(int src){ memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i) d[i] = INF; d[src] = 0; vis[src] = true; q.push(src); while(!q.empty()){ int x = q.front(); q.pop(); vis[x] = false; for(int e=first[x]; e!=-1; e=next[e]){ if(d[x]+w[e] < d[v[e]]){ d[v[e]] = d[x]+w[e]; if(!vis[v[e]]){ vis[v[e]] = true; q.push(v[e]); } } } } }int main(){ int a,b,c; while(~scanf("%d%d",&n,&m)&&n+m){ read_graph(); SPFA(1); printf("%d\n", d[n]); } return 0;}
4. Floyd algorithm
# Include <cstdio> # include <cstring> # include <utility> # include <queue> using namespace STD; const int n = 105; const int INF = 2147483646; int N, m, d [N] [N]; inline void read_graph () {for (INT I = 1; I <= N; ++ I) {d [I] [I] = inf; For (Int J = I + 1; j <= N; ++ J) d [I] [J] = d [J] [I] = inf;} int A, B, C; For (int e = 1; E <= m; ++ e) {scanf ("% d", & A, & B, & C ); d [a] [B] = d [B] [a] = C ;}} inline void Floyd (INT SRC) {for (int K = 1; k <= N; ++ K) {for (in T I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) if (d [I] [k] <INF & D [k] [J] <inf) {// prevent overflow d [I] [J] = min (d [I] [J], d [I] [k] + d [k] [J]) ;}}} int main () {int A, B, C; while (~ Scanf ("% d", & N, & M) & N + M) {read_graph (); Floyd (1); printf ("% d \ n ", d [1] [N]);} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)