Meaning Chinese
The most basic Shortest Path. Note that the edges may be minimized multiple times.
# Include <cstdio> # include <cstring> using namespace STD; const int n = 205, M = 1005; int mat [N] [N], V [N], d [N], n, m, S, T; void Dijkstra () {memset (D, 0x3f, sizeof (d); memset (v, 0, sizeof (v); For (INT I = d [s] = 0; I <n; ++ I) {int cur = N; // d [N] Is INF, and cur is the currently unlabeled point with the minimum distance to S. For (Int J = 0; j <n; ++ J) if (! V [J] & D [J] <D [cur]) cur = J; V [cur] = 1; if (cur = T) return; for (Int J = 0; j <n; ++ J) {If (d [J]> d [cur] + mat [cur] [J]) d [J] = d [cur] + mat [cur] [J] ;}} int main () {int A, B, X; while (~ Scanf ("% d", & N, & M) {memset (MAT, 0x3f, sizeof (MAT); For (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B, & X); If (MAT [a] [B]> X) // note that there may be multiple mat [a] [B] = mat [B] [a] = x;} scanf ("% d", & S, & T); Dijkstra (); If (d [T]> = d [N]) printf ("-1 \ n "); else printf ("% d \ n", d [T]);} return 0 ;}
Smooth engineering resumption
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
HDU 1874 smooth engineering continued (Basic short circuit)