首页 > 代码库 > Floyd
Floyd
#include <stdio.h>#include <string.h>#define maxn 100#define INF -1int 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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。