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 code:# Include <iostream> # include <cstdio> # include <cstring> # define Maxx 99999999 # include <queue> using namespace STD; struct node {int to, W; int next; // indicates the next edge of the same starting point} edge [20005]; int vis [505], DIS [505]; int pre [20005]; int M, N; int tot; void addedge (int u, int V, int W) {Int J, T = 0; For (j = pre [u]; J! =-1; j = edge [J]. next) // remove the duplicate edge {If (V = edge [J]. to & W> = edge [J]. w) {T = 1; break;} If (t = 1) return; edge [tot]. to = V; edge [tot]. W = W; edge [tot]. next = pre [u]; Pre [u] = tot ++; edge [tot]. to = u; // construct the reverse edge [tot]. W = W; edge [tot]. next = pre [v]; Pre [v] = tot ++; return;} int spfa () {queue <int> q; int now, next; for (INT I = 1; I <= m; I ++) {vis [I] = 0; DIS [I] = Maxx;} dis [1] = 0; vis [1] = 1; q. push (1); While (! Q. empty () {now = Q. front (); q. pop (); vis [now] = 0; // reverse edge for (INT I = pre [now]; I! =-1; I = edge [I]. next) // traverse all the edges at the same starting point {int v = edge [I]. to; If (DIS [v]> dis [now] + edge [I]. w) {dis [v] = dis [now] + edge [I]. w; If (! Vis [v]) {vis [v] = 1; q. Push (v) ;}}} return dis [m] ;}int main () {While (~ Scanf ("% d", & M, & N) {If (M = 0 & n = 0) break; Tot = 1; memset (PRE, -1, sizeof (pre); While (n --) {int A, B, C; scanf ("% d", & A, & B, & C); addedge (a, B, c) ;}int S = spfa (); cout <S <Endl;} return 0 ;}
HDU 2544 short-circuited spfa list template