Floyd求多源最短路
作者:
阿符长命十万岁
,
2024-04-12 18:35:44
,
所有人可见
,
阅读 6
#include <iostream>
using namespace std;
const int N = 210, M = 1e9 + 10;
int f[N][N];
int n, m, k;
void floyd() {
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]);
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (i == j)
f[i][j] = 0;
else
f[i][j] = M;
while (m--) {
int x, y, z;
cin >> x >> y >> z;
f[x][y] = min(f[x][y], z);
}
floyd();
while (k--) {
int x, y;
cin >> x >> y;
if (f[x][y] > M / 2)cout << "impossible\n";
else cout << f[x][y] << '\n';
}
return 0;
}