赞
踩
使用邻接矩阵来存储图的信息,然后使用三重for循环实现弗洛伊德算法。
- #include <iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int inf = 1 << 26;
- int main()
- {
- int map[11][11];
- int m,n,i, j, w;
-
- for (int i = 1; i <= 10; i++) {
- for (int j = 1; j <= 10; j++) {
- if (i == j) map[i][j] = 0;
- else map[i][j] = inf;
- }
- }
- scanf("%d %d", &n, &m);
- for (int u = 1; u <= m; u++) {
- scanf("%d %d %d", &i, &j, &w);
- map[i][j] = w;
- map[j][i] = w;
- }
- for (int k = 1; k <= n; k++) {
-
- for (int i = 1; i <= n; i++) {
- if (map[i][k] == inf) continue;
- for (int j = 1; j <= n; j++) {
- if (i==j)continue;
- map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
- }
- }
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- cout << map[i][j] << " ";
- }
- cout << endl;
- }
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。