AcWing 854. Floyd求最短路 C
原题链接
简单
作者:
LaChimere
,
2021-05-31 09:54:38
,
所有人可见
,
阅读 214
C
#include <stdio.h>
#include <string.h>
#define MAXSIZE 210
#define INF 0x3f3f3f3f
#define min(a,b) ((a)<(b)?(a):(b))
int n, m, k, dist[MAXSIZE][MAXSIZE];
void init() {
scanf("%d%d%d", &n, &m, &k);
memset(dist, 0x3f, sizeof(dist));
for (int i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
int x, y, z;
while (m--) {
scanf("%d%d%d", &x, &y, &z);
dist[x][y] = min(dist[x][y], z);
}
}
void Floyd() {
for (int u = 1; u <= n; ++u) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (dist[i][u] != INF && dist[u][j] != INF) {
dist[i][j] = min(dist[i][j], dist[i][u]+dist[u][j]);
}
}
}
}
}
int main() {
init();
Floyd();
int x, y;
while (k--) {
scanf("%d%d", &x, &y);
if (dist[x][y] == INF) {
puts("impossible");
} else {
printf("%d\n", dist[x][y]);
}
}
return 0;
}