AcWing 854. Floyd求最短路(-_- ~~~^v^)
原题链接
简单
作者:
Akac
,
2021-03-06 20:06:16
,
所有人可见
,
阅读 467
C++ 代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int dist[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 ++ )
dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);
}
int main()
{
memset(dist,INF,sizeof dist);
cin >> n >> m >> k;
for(int i = 0 ; i < m ; i ++ )
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
dist[a][b] = min(dist[a][b], c);
}
for(int i = 1 ; i <= n ; i++ ) dist[i][i] = 0;
floyd();
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
if(dist[a][b]>=INF/2) puts("impossible");
else cout << dist[a][b] << endl;
}
return 0;
}