You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#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;
|
|
|
|
|
}
|