8.2.Floyd's Algorithm for the All-Pairs Shortest-Paths Problem

/*
 *	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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值