Files
python/TangDou/AcWing/Floyd/854.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
int n, m, k;
int d[N][N];
// 算法结束后,d[a][b]表示a到b的最短距离
void floyd() {
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]);
}
int main() {
cin >> n >> m >> k;
// floyd初始化
memset(d, 0x3f, sizeof d); // 任意两点间距离正无穷
for (int i = 0; i < N; i++) d[i][i] = 0; // 自己和自己是距离为0的
// 读入数据
while (m--) {
int a, b, c;
cin >> a >> b >> c;
d[a][b] = min(d[a][b], c); // 保留最短边.(可能有重边,保留最短边)
}
// 调用floyd
floyd();
// 处理所有询问
while (k--) {
int a, b;
cin >> a >> b;
// 由于有负权边存在所以约大过INF/2也很合理
if (d[a][b] > INF / 2)
puts("impossible");
else
printf("%d\n", d[a][b]);
}
return 0;
}