Floyd求最短路

Floyd求最短路

#include <bits/stdc++.h>
using namespace std;

const int N = 210;
const int 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]);
}

signed main(){
    cin >> n >> m >> k;
    
    memset(g, 0x3f, sizeof g);
    for(int i = 1; i <= n; ++i) g[i][i] = 0;
    
    for(int i = 1; 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] > 0x3f3f3f3f / 2) puts("impossible");
        else cout << g[x][y] << endl;
    }
    
    return 0;
}
posted @ 2025-04-07 19:58  awei040519  阅读(15)  评论(0)    收藏  举报