给出一张由n个点m条边组成的无向图,求所有点对(i,j)之间的最短路。
n<=100; m<=4500; 1<=w<=1000
多源最短路模板题,注意循环顺序是kij,另外可能会有重边,因此两点之间的距离要初始化为inf,读入边权时取最小值。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
const int inf = 1000000000;
int d[105][105];
void solve() {
int n, m;
cin >> n >> m;
rep(i,1,n) rep(j,1,n) {
d[i][j] = i==j ? 0 : inf;
}
rep(i,1,m) {
int u, v, w;
cin >> u >> v >> w;
d[u][v] = d[v][u] = min(d[u][v], w);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
}
}
rep(i,1,n) {
rep(j,1,n) cout << d[i][j] << " ";
cout << "\n";
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:int,短路,lgB3647,long,Floyd,inf,rep
From: https://www.cnblogs.com/chenfy27/p/18090886