/*
* 8.2.Floyd's Algorithm for the All-Pairs Shortest-Paths Problem
* 2011/08/16
* ArtWalk
*/
/*
a b c d a b c d
a 0 $ 3 $ a 0 10 3 4
W= b 2 0 $ $ D = b 2 0 5 6
c $ 7 0 1 c 7 7 0 1
d 6 $ $ 0 d 6 16 9 0
*/
#include <iostream>
#define N 5
#define $ 65536
using namespace std;
void Floyd( int a[N][N] );
void display( int a[N][N] );
inline int min( int a, int b );
int main(int ac, char** av)
{
int a[N][N] = {
{ 0 },
{ 0, 0, $, 3, $ },
{ 0, 2, 0, $, $ },
{ 0, $, 7, 0, 1 },
{ 0, 6, $, $, 0 }
};
display( a );
Floyd(a);
// display( a );
return 0;
}
void Floyd( int a[N][N] ) {
for ( int k = 1; k != N; ++k ) {
for ( int i = 1; i != N; ++i ) {
for ( int j = 1; j != N; ++j ) {
// here is the key
a[i][j] = min( a[i][j], a[i][k] + a[k][j] );
}
}
display( a );
}
}
inline int min( int a, int b ) {
return ( a < b ? a : b );
}
void display( int a[N][N] ) {
for ( int i = 1; i != N; ++i ) {
for ( int j = 1; j != N; ++j ) {
if ( a[i][j] == 65536 ) {
cout << "$ ";
} else {
cout << a[i][j] << " ";
}
}
cout << endl;
}
cout << endl;
}
8.2.Floyd's Algorithm for the All-Pairs Shortest-Paths Problem
最新推荐文章于 2022-05-11 23:23:55 发布