图论 - 最短路 - Floyd

图论 - 最短路 - Floyd

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

代码:

#include 

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;
}

你可能感兴趣的:(图论 - 最短路 - Floyd)