AcWing 854. Floyd求最短路
原题链接
简单
作者:
dsyami
,
2021-06-05 19:03:31
,
所有人可见
,
阅读 219
#include <iostream>
#include <cstring>
using namespace std;
const int N = 210;
int g[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 ++ )
{
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
int main()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
{
if (i == j) g[i][j] = 0;
else g[i][j] = 0x3f3f3f3f;
}
for (int i = 0; i < m; i ++ )
{
int x, y, z;
cin >> x >> y >> z;
g[x][y] = min(g[x][y], z);
}
floyd();
while (k -- )
{
int a, b;
cin >> a >> b;
if (g[a][b] > 0x3f3f3f3f / 2) puts("impossible");
else cout << g[a][b] << endl;
}
return 0;
}