Smooth engineering resumption
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 29834 accepted submission (s): 10894
Problem description a province has finally built many roads since its smooth engineering plan was implemented for many years. However, when there are too many roads, there are many ways to choose from every town to another town, some solutions are much shorter than others. This makes pedestrians very difficult.
Now you know the start and end points. Calculate the shortest distance from the start point to the end point.
The input question contains multiple groups of data. Please process the data until the end of the file.
The first row of each data group contains two positive integers n and M (0 <n <1000, 0 <m <), representing the number of existing towns and the number of constructed roads respectively. The towns are 0 ~ N-1 number.
Next is m-line road information. Each row has three integers, A, B, x (0 <= A, B <n,! = B, 0 <x <10000) indicates that there is a two-way road with x length between town a and town B.
The next line has two integers, T (0 <= S, T <n), representing the start point and the end point respectively.
For each group of data, output the shortest distance to walk in one row. If there is no route from S to T, output-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample output
2-1/* For the first time, use the Floyd algorithm to solve the problem. Note that the INF definition is neither too big nor too small. If there are up to n edges and the maximum size of each directed edge is m, it is appropriate to define the INF size as M * n. */# Include <stdio. h >#include <algorithm> # define INF 10000000 using namespace STD; int s [210] [210]; int M, N; int Floyd () // Floyd algorithm. {Int I, j, k; For (k = 0; k <n; k ++) for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) s [I] [J] = min (s [I] [J], s [I] [k] + s [k] [J]);} int main () {int I, j, X, A, B, C, S, T; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <n; I ++) // initializes the array. A [I] [J] indicates the distance from I to J. For (j = 0; j <n; j ++) {if (I = J) s [I] [J] = 0; else s [I] [J] = inf ;}for (I = 0; I <m; I ++) {scanf ("% d ", & A, & B, & C); If (C <s [a] [B]) s [a] [B] = s [B] [a] = C;} scanf ("% d", & S, & T); Floyd (); if (s [s] [T] = inf) printf ("-1 \ n"); else printf ("% d \ n ", s [s] [T]);} return 0 ;}
Continued smooth Engineering (hangdian 1874)