Report on shortest path based on freyd pku1125

Source: Internet
Author: User

The freuch algorithm is a good algorithm for finding the minimum path, but the algorithm requires a high time complexity. It can be used when there are few nodes:

Problem description: it is known that a weighted directed graph with each edge weight greater than 0 contains N vertices, for each vertex Vi! = VJ. The shortest path and shortest path length between each pair of vertices must be obtained.
Solution:Floyd algorithm

A0 1 2 3 A1 1 2 3
1 0 4 5 1 0 4 5
2 2 0 6 2 2 0 Min (6, 2 + 5)
3 2 2 0 3 2 Min (2, 2 + 4) 0

A2 1 2 3 A3 1 2 3
1 0 4 Min (5, 4 + 6) 1 0 Min (4,5 + 2) 5
2 2 0 6 2 Min (2, 6 + 2) 0 6
3 Min (2, 2 + 2) 2 0 3 2 2 0

Here, AI is the shortest path (minimum value) between x and y when I is used as the intermediate amount. For example, A1 is the minimum value of 2-3-2 when 1 is used as the intermediate value, in this way, when running to A3, the shortest path from X to Y is obtained, the maximum value in each row is the value of all pivot points for the number calculated by this row (when the value is infinite (1200 is infinite here), it means that the maximum value in each row is the minimum value, this is the solution to the problem.

Core code:

For (int K = 1; k <= N; k ++ )// Generate a loop of A0, A1, a2... For (INT I = 1; I <= N; I ++ )// Line For (Int J = 1; j <= N; j ++ )// Column // If it is I = k | j = k | I = J, it remains unchanged; otherwise, the minimum value is used. Array [I] [J] = (I = k | j = k | I = J )? Array [I] [J]: (Array [I] [J] <(array [I] [k] + array [k] [J])? Array [I] [J] :( array [I] [k] + array [k] [J]);

Finally, based on the question, take the minimum value in the maximum value of each line.

NOTE: Special processing should be performed on the values on the diagonal line, so that all of them are 0. If they cannot be reached, they are set to 1200.

Code:

# Include <stdio. h>
# Include <memory. h>
Main (){
Int I, N;
Int A [101] [101], Max, M [101], flag, J, K, A1, N1, B;
Int min;
While (scanf ("% d", & N ){
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
A [I] [J] = (I = J )? 0:-1; // outside the diagonal line, the other values are only-1, and the diagonal value is 0;
For (I = 1; I <= N; I ++ ){
Scanf ("% d", & N1 );
While (N1 --){
Scanf ("% d", & A1, & B );
A [I] [a1] = B;
}
}
For (k = 1; k <= N; k ++ ){
For (j = 1; j <= N; j ++)
If (A [k] [J] =-1)
A [k] [J] = 1200; // if there is no path between two points, the value is set to 1200 (infinity );
}
For (I = 1; I <= N; I ++)
For (k = 1; k <= N; k ++)
For (j = 1; j <= N; j ++)
A [k] [J] = (I = k | I = j | K = J )? A [k] [J] :( (A [k] [J] <(A [k] [I] + A [I] [J])? A [k] [J] :( A [k] [I] + A [I] [J]); // template code of Floyd
For (I = 1; I <= N; I ++ ){
Max = 0;
For (j = 1; j <= N; j ++)
Max = max> A [I] [J]? MAX: A [I] [J];
M [I] = max;
}
Flag = 0;
Min = 1200;
For (I = 1; I <= N; I ++ ){

If (min> M [I]) {
Min = m [I];
Flag = I;
}
}
If (FLAG)
Printf ("% d/N", flag, min );
Else
Printf ("disjoint/N ");
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.