图论 - 最短路 - Floyd

邮差的信 提交于 2019-12-04 10:28:58

图论 - 最短路 - Floyd

题目链接: https://www.luogu.org/problem/P2910

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 110;

const int M = 10010;

int n, m;

int dis[N][N], ord[M];

void Floyd() {
    for (int k = 1; k <= n; k ++ ) {
        for (int i = 1; i <= n; i ++ ) {
            for (int j = 1; j <= n; j ++ ) {
                if (dis[i][j] > dis[i][k] + dis[k][j]) {
                    dis[i][j] = dis[i][k] + dis[k][j];
                }
            }
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++ ) {
        scanf("%d", &ord[i]);
    }
    for (int i = 1; i <= n; i ++ ) {
        for (int j = 1; j <= n; j ++ ) {
            scanf("%d", &dis[i][j]);
        }
    }
    Floyd();
    int ans = 0;
    for (int i = 1; i < m; i ++ ) {
        ans += dis[ord[i]][ord[i + 1]];
    }
    printf("%d\n", ans);
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!