题目链接:https://www.luogu.com.cn/problem/B3647
floyd算法:https://oi-wiki.org/graph/shortest-path/#floyd-算法
示例程序:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 101;
int n, m, f[maxn][maxn];
int main() {
scanf("%d%d", &n, &m);
memset(f, 0x3f, sizeof f);
for (int i = 1; i <= n; i++)
f[i][i] = 0;
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
f[u][v] = f[v][u] = w;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j > 1) putchar(' ');
printf("%d", f[i][j]);
}
puts("");
}
return 0;
}
标签:洛谷,int,题解,多汇,算法,floyd,maxn,B3647
From: https://www.cnblogs.com/quanjun/p/17928959.html