1 条题解

  • 0
    @ 2026-8-18 12:44:31

    `#include #include using namespace std;

    const int MAXN = 105; const long long INF = 0x3f3f3f3f3f3f3f3fLL;

    long long dist[MAXN][MAXN];

    int main() { ios::sync_with_stdio(false); cin.tie(0);

    int n, m;
    cin >> n >> m;
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dist[i][j] = INF;
        }
        dist[i][i] = 0;
    }
    
    for (int i = 0; i < m; i++) {
        int u, v;
        long long w;
        cin >> u >> v >> w;
        if (w < dist[u][v]) {
            dist[u][v] = w;
            dist[v][u] = w;
        }
    }
    
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            if (dist[i][k] == INF) continue;
            for (int j = 1; j <= n; j++) {
                if (dist[k][j] == INF) continue;
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << dist[i][j];
            if (j < n) cout << ' ';
        }
        cout << '\n';
    }
    
    return 0;
    

    } `

    • 1

    信息

    ID
    3410
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    95
    已通过
    18
    上传者