0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

854. Floyd求最短路

https://www.acwing.com/problem/content/submission/code_detail/6498911/
This commit is contained in:
Baoshuo Ren 2021-07-17 19:35:19 +08:00 committed by Baoshuo Ren
parent d481cadb74
commit 7741c40d36
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

34
AcWing/854/854.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, z, f[205][205];
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
f[i][j] = i == j ? 0 : 0x3f3f3f3f3f;
}
}
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
f[x][y] = min(f[x][y], z);
}
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 <= k; i++) {
cin >> x >> y;
if (f[x][y] > 0x1fffffff) {
cout << "impossible" << endl;
} else {
cout << f[x][y] << endl;
}
}
return 0;
}