码迷,mamicode.com
首页 > 其他好文 > 详细

Floyd

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

标签:floyd

 

#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

标签:floyd

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!