首页 > 其他 > 详细

Floyd

时间:2014-07-28 16:12:05      阅读:316      评论:0      收藏:0      [点我收藏+]

 

#include <stdio.h>
#include <string.h>
#define maxn 100
#define INF -1

int map[maxn][maxn];
int n, m, path[maxn][maxn];

void Floyd(int n)
{
	int i, j, k;
	for(k = 0; k < n; ++k)
		for(i = 0; i < n; ++i)
			for(j = 0; j < n; ++j)
				if(map[i][k] != INF && map[k][j] != INF && (map[i][k] + map[k][j] < map[i][j] || map[i][j] == INF)){
					map[i][j] = map[i][k] + map[k][j];
					path[i][j] = k;
				}			
}

void getPath(int v, int u)
{
	int k = path[v][u];
	if(k == INF){
		printf("%d===>", v);
		return;
	}
	getPath(v, k);
	getPath(k, u);
}

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	scanf("%d%d", &n, &m);
	memset(map, INF, sizeof(map));
	memset(path, INF, sizeof(path));
	int i, j, a, b, c;
	for(i = 0; i < n; ++i) map[i][i] = 0;
	for(i = 0; i < m; ++i){
		scanf("%d%d%d", &a, &b, &c);
		map[a][b] = c;
	}
	Floyd(n);
	for(i = 0; i < n; ++i)
		for(j = 0; j < n; ++j)
			if(map[i][j] != INF){
				printf("%d->%d:%d\n the path is:", i, j, map[i][j]);
				getPath(i, j);
				printf("%d\n", j);
			}
	return 0;
}


 

Floyd,布布扣,bubuko.com

Floyd

原文:http://blog.csdn.net/chang_mu/article/details/38226073

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!