結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-01 16:06:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 1,777 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 60,664 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 05:02:39
合計ジャッジ時間 1,852 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 15 ms
5,376 KB
testcase_05 AC 18 ms
5,376 KB
testcase_06 AC 20 ms
5,376 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 13 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 11 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 10 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 12 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 12 ms
5,376 KB
testcase_26 AC 11 ms
5,376 KB
testcase_27 AC 10 ms
5,376 KB
testcase_28 AC 24 ms
5,376 KB
testcase_29 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int kINF = 1 << 28;
const int kMAX_N = 210;
const int kMAX_M = kMAX_N * (kMAX_N - 1) / 2;

int N, M, S, G;
int cost[kMAX_N][kMAX_N];
int next_node[kMAX_N][kMAX_N];

int a[kMAX_M], b[kMAX_M], c[kMAX_M];

void WarshallFloyd() {
    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (cost[i][j] > cost[i][k] + cost[k][j]) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                    next_node[i][j] = next_node[i][k];
                } else if (cost[i][j] == cost[i][k] + cost[k][j] && next_node[i][j] > next_node[i][k]) {
                    next_node[i][j] = next_node[i][k];
                }
            }
        }
    }
}

vector<int> RestoreRoute(int node, int goal) {
    vector<int> route;
    route.push_back(node);
    do {
        node = next_node[node][goal];
        route.push_back(node);
    } while (node != goal);
    return route;
}


void Solve() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j) {
                cost[i][j] = 0;
                next_node[i][j] = kINF;
            } else {
                cost[i][j] = kINF;
                next_node[i][j] = j;
            }
        }
    }

    for (int i = 0; i < M; i++) {
        cost[a[i]][b[i]] = cost[b[i]][a[i]] = c[i];
    }

    WarshallFloyd();

    vector<int> route = RestoreRoute(S, G);

    for (int i = 0; i < route.size(); i++) {
        cout << (i == 0 ? "" : " ") << route[i] << (i == route.size() - 1 ? "\n" : "");
    }
}

int main() {
    cin >> N >> M >> S >> G;

    for (int i = 0; i < M; i++) {
        cin >> a[i] >> b[i] >> c[i];
    }

    Solve();

    return 0;
}
0