标签: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
原文地址:http://blog.csdn.net/chang_mu/article/details/38226073