多源路最短路问题
作者:
巷港
,
2022-03-03 18:31:12
,
所有人可见
,
阅读 204
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 210;
const int INF = 1000000;
int dist[N][N];
int n,m,q;
void floyd()
{
for (int k=1;k<=n;k++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (i==j) dist[i][j]=0;
else dist[i][j]=INF;
}
}
while (m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
dist[a][b]=min(dist[a][b],c);
}
floyd();
while (q--)
{
int x,y;
scanf("%d%d",&x,&y);
if (dist[x][y]>INF/2) puts("impossible");
else printf("%d\n",dist[x][y]);
}
return 0;
}