Shortest Path
Time Limit: 5000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 33657 accepted submission (s): 14617
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
# Include <iostream> # include <stdio. h ># include <string >#include <cstring> # include <algorithm> # define n 1000 # define INF 0x3f3f3fusing namespace STD; int n, m; int U, V, W; int map [N] [N]; int vis [N]; int ans; int dis [N]; // indicates the distance from the current node to any point, that is, void Dijkstra () {memset (DIS, INF, sizeof dis); memset (VIS, 0, sizeof vis); int I, j; int now, mid; dis [1] = 0; For (INT I = 1; I <= N; I ++) {mid = inf; For (INT I = 1; I <= N; I ++) {If (! Vis [I] & Mid> dis [I]) {mid = dis [I]; now = I ;}} vis [now] = 1; for (INT I = 1; I <= N; I ++) {If (DIS [I]> dis [now] + map [now] [I]) dis [I] = dis [now] + map [now] [I] ;}} ans = dis [N];} int main () {While (scanf ("% d", & N, & M), m + n) {memset (MAP, INF, sizeof map ); for (INT I = 1; I <= m; I ++) {scanf ("% d", & U, & V, & W ); map [u] [v] = map [v] [u] = W;} Dijkstra (); cout <ans <Endl;} return 0 ;}
HDU 2544 short-circuit Dijkstra Template