Time limit:1000 ms
Memory limit:32768kb
64bit Io format:% I64d & % i64usubmit status
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?
Input
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, C (1 & lt; = 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
Output a line for each group of inputs, 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. This is the most short-circuit problem, but it is still difficult for me to use this dish. In fact, each algorithm can be fully understood by understanding the basics. About the shortest dij algorithm, I asked Wang about five times ...... Cai Ah = calculate the shortest short circuit from the start point to each point, and then use greedy to update the shortest circuit to this node
If (! Vis [J] & dij [p] + map [p] [J] <dij [J]) determine whether it is the shortest path {
Dij [J] = dij [p] + map [p] [J];}
1 # include <cstdio> 2 # include <string. h> 3 using namespace STD; 4 # define INF 1000000000 5 Int map [1000] [1000]; 6 int n, m; 7 int dij [10010]; 8 int vis [10010]; 9 void dij (INT y, int X) 10 {11 int I, P, J, min; 12 for (I = 1; I <= y; I ++) 13 {14 dij [I] = map [1] [I]; 15 vis [I] = 0; // initialization, whether it is accessed 16} 17 vis [x] = 1; // mark the accessed path 18 for (I = 1; I <= y; I ++) 19 {20 min = inf; // the Shortest Path 21 for (j = 1; j <= y; j ++) 22 {23 if (! Vis [J] & dij [J] <min) 24 {25 p = J; 26 min = dij [J]; 27} 28} 29 vis [p] = 1; // mark the accessed path 30 for (j = 1; j <= y; j ++) 31 {32 If (! Vis [J] & dij [p] + map [p] [J] <dij [J]) // determine whether it is the shortest path. 33 {34 dij [J] = dij [p] + map [p] [J]; // update it in time !!! 35} 36} 37} 38} 39 int main () 40 {41 // int n, m; 42 int A, B, T; 43 while (scanf ("% d", & N, & M) & (n! = 0 & M! = 0) 44 {45 for (INT I = 1; I <= N; I ++) 46 for (Int J = 1; j <= N; j ++) 47 {48 map [I] [J] = inf; // If INF is infinite, this path cannot be found. 49} 50 for (INT I = 1; I <= m; I ++) // M indicates a total of M channels 51 {52 scanf ("% d", & A, & B, & T ); 53 map [a] [B] = map [B] [a] = T; // This path is common, and this figure is an undirected graph 54} 55 dij (n, 1 ); // Dijkstra algorithm 56 printf ("% d \ n", dij [N]); end point is N, calculate the shortest path from 1 to n 57} 58 return 0; 59}