[HDU 2544] the shortest path (dijela template), hdu2544
Problem DescriptionIn 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?InputThe 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.OutputOutput a line for each group of inputs, indicating the shortest time for a staff member to walk from the store to the stadiumSample Input2 11 2 33 31 2 52 3 53 1 20 0Sample Output3 2
# Include <bits/stdc ++. h> using namespace std; # define inf 99999999int main () {int n, m, u, v, w, I, j, e [105] [105], dis [105], book [105], minn; while (cin> n> m, n | m) {for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) if (I = j) e [I] [j] = 0; else e [I] [j] = inf; for (I = 1; I <= m; I ++) {scanf ("% d ", & u, & v, & w); e [u] [v] = e [v] [u] = w;} for (I = 1; I <= n; I ++) dis [I] = e [1] [I]; for (I = 1; I <= n; I ++) book [I] = 0; book [1] = 1; for (I = 1; I <= n-1; I ++) {minn = inf; for (j = 1; j <= n; j ++) // find the point closest to the origin {if (book [j] = 0 & dis [j] <minn) {minn = dis [j]; u = j ;}} book [u] = 1; for (v = 1; v <= n; v ++) {if (e [u] [v] <inf & dis [v]> dis [u] + e [u] [v]) dis [v] = dis [u] + e [u] [v] ;}} cout <dis [n] <endl;} return 0 ;}