folyd算法
作者:
zzu
,
2024-05-15 22:54:59
,
所有人可见
,
阅读 2
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;//?
int n, m, Q;
int dist[N][N];
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]);//i->j 与 i->k->j 哪个小
}
int main()
{
cin >> n >> m >> Q;
memset(dist, 0x3f, sizeof dist);
for(int i = 1; i <=n ; i ++) dist[i][i] = 0;//刚开始每个点都可以到达自己所以=0
while(m --)
{
int a, b, w;
cin >> a >> b >> w;
dist[a][b] = min(dist[a][b], w);
}
floyd();
while(Q --)
{
int a, b;
cin >> a >> b;
if(dist[a][b] > INF / 2) puts("impossible");// 因为会有INF - 2, 所有不一定是INF
else cout << dist[a][b] << endl;
}
return 0;
}