Floyd algorithm
Core code:
. 1 for (K = 0 ; K <n-;; K ++ ) 2 for (I = 0 ; I <n-; I ++ ) . 3 for (J = 0 ; J <n-; J ++ ) . 4 IF (A [I] [K] + A [K] [J] < A [I] [J]) . 5 { . 6 A [I] [J] = A [I] [K] + A [K] [J]; . 7 path [I] [ J] = path [k] [J]; // shorten the path length, to bypass the k J . 8 } . 9 // a [i] [J] is the shortest path length between the vertices i and j, path [i] [j] j is the corresponding previous vertex path 10 // vertex number of the vertex
Analyzing triple loop for adding an intermediate point k, scan, to find the shortest path. Time complexity of O (n ^ 3), the intermediate vertex major increase, try to determine whether the path is less than the original length, is less than, then the new path in order to replace the original route, modification matrix elements.
Simple Case:
In the annual school competition, all students finalists will receive a very nice t-shirt. But whenever our staff to hundreds of pieces of clothing shipped back to the stadium from the store when they are very tired! So now they want to find the shortest route to the stadium from the store, you can help them?
Comprising a plurality of sets of input data. The first line of each two integers N, M (N <= 100, M <= 10000), N represents a few street intersections Chengdu, reference numeral 1 is to store the location of the intersection, the intersection designated N is the location of the stadium, M indicates a couple of choices in Chengdu. N = M = 0 indicates the end of input. Next M rows, each row comprising three integers A, B, C (1 <= A, B <= N, 1 <= C <= 1000), indicates there is a path between the intersection A and the intersection B, we staff need to C-minute time traveled this road. Input line to ensure the presence of at least one track to store. For each input and output line, it represents the track workers come from the store minimum time
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3 2
#include <the iostream> #define MAXN 0x3f3f3f3f the using namespace STD; int Lowcost [ 1000 ] [ 1000 ], I, J, K, n-; // global variables, initializes all variables void Floyd () { for (K = . 1 ; K <= n-; K ++ ) for (I = . 1 ; I <= n-; I ++ ) for (J = . 1 ; J <= n-; J ++ ) IF (Lowcost [I] [K] + Lowcost [K] [J ] < Lowcost [I] [J]) Lowcost [I] [J] = Lowcost [I] [K] + Lowcost [K] [J]; } int main () { int m, c, a, B; the while (CIN >> >> n-m) // n-represents the number of blocks, m for large ones, c from a representative of the number of minutes between B { IF (n-== m && n-== 0 ) BREAK ; for (I = . 1 ; I <= n-; I ++ ) for (J = . 1 ; J <= n-; J ++ ) { IF (I == J) Lowcost [I] [ J] = 0 ; // i.e. slash set to 0, the value of a weight to a 0 the else Lowcost [I] [J] = MAXN; // the weight value is set infinite array } for (I = . 1; I <= m; I ++ ) { CIN >> >> A >> B c; // input path (i.e., an array subscript) a, b and c are weight lowcost [a] [b] = lowcost [b] [ a] = C; // storing path labeled a, b lien array value C } floyd (); // call floyd shortest path algorithm COUT << Lowcost [ . 1 ] [n-] << endl ; } return 0 ; }