Shortest Path
Time Limit: 5000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 30972 accepted submission (s): 13345
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
A pure short path entry question.
Dijkstra:
# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> # include <algorithm> # include <queue> # include <vector> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 10000; int n, m; int X, Y, cost; int edge [maxn] [maxn]; // adjacent matrix int vist [maxn]; // records whether the vertex is in the Set int dis [maxn]; // records the weight value void Init () {for (INT I = 1; I <= N; I ++) {vist [I] = 0; DIS [I] = inf; For (Int J = 1; j <= N; j ++) edge [I] [J] = inf;} For (int I = 1; I <= m; I ++) {scanf ("% d", & X, & Y, & cost ); edge [x] [Y] = edge [y] [x] = Cost ;}} void Dijkstra (INT V0) {dis [V0] = 0; vist [V0] = 1; // vertex 1 such as set for (INT I = 2; I <= N; I ++) // obtain the shortest path {int min = inf; int K; For (Int J = 1; j <= N; j ++) {If (! Vist [J] & dis [J] <min) {min = dis [J]; k = J ;}} vist [k] = 1; // vertex K enters the set for (Int J = 1; j <= N; j ++) {If (! Vist [J] & edge [k] [J] <INF & dis [J]> dis [k] + edge [k] [J]) dis [J] = dis [k] + edge [k] [J] ;}} int main () {While (scanf ("% d", & N, & M) = 2 & N & M) {Init (); Dijkstra (1); printf ("% d \ n", DIS [N]);} return 0 ;}
Floyd:
#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<algorithm>#include<vector>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 105;int map[maxn][maxn];int main(){ int n, m; while( scanf("%d%d", &n, &m)==2 &&n &&m ) { int a, b, c; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) map[i][j] = INF; for(int i=1; i<=m; i++) { scanf("%d%d%d", &a, &b, &c); map[a][b] = map[b][a] = c; } for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) map[i][j] = min( map[i][j], map[i][k] + map[k][j] ); printf("%d\n", map[1][n]); } return 0;}