其他分享
首页 > 其他分享> > 【Floyd】AcWing854.Floyd求最短路

【Floyd】AcWing854.Floyd求最短路

作者:互联网

AcWing854.Floyd求最短路

题解

注意:k, i, j的顺序不能改变,必须以k作为桥梁

#include <iostream>
#include <cstring>
using namespace std;

const int N = 210;

int g[N][N], n, m, k;

int main()
{
    cin >> n >> m >> k;
    
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(i == j) g[i][j] = 0; //删去自环,不让自环成为一条大于0的路径
            else g[i][j] = 0x3f3f3f3f;
    
    int a, b, c;
    for(int i = 0; i < m; ++i)
    {
        cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);  //因为有重边
    }
    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]);
    for(int i = 0; i < k; ++i)
    {
        cin >> a >> b;
        if(g[a][b] >= 0x3f3f3f3f/2) cout << "impossible" << endl;
        else cout << g[a][b] << endl;
    }
    return 0;
}

标签:int,题解,短路,Floyd,AcWing854,include
来源: https://www.cnblogs.com/czy-algorithm/p/16324309.html