floyd算法用来求多源汇最短路
用邻接矩阵来存所有的边
时间复杂度O(n^3)
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 20010,INF = 1e9;
int n,m,k;
int g[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 ++ ){
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 <= m;j ++ ){
if(i == j) g[i][j] = 0;
else g[i][j] = INF;
}
}
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 x,y;
cin >> x >> y;
if(g[x][y] > INF / 2) cout << "impossible" << endl; //INF还是要/2,考虑到可能有负权边的情况
else cout << g[x][y] <<endl;
}
return 0;
}