結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-01 15:50:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,380 bytes
コンパイル時間 405 ms
コンパイル使用メモリ 56,468 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 05:02:16
合計ジャッジ時間 1,562 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
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] = k;
                }
            }
        }
    }
}

/*
void RestoreRoute(int node, int start, int goal) {
    cout << node << " " << start << " " << goal << endl;
    if (node == start) {
        cout << node;
        return;
    }
    RestoreRoute(next_node[start][node], start, goal);
    cout << " " << node;
    if (node == goal) {
        cout << endl;
    }
}
*/

/*
#include <vector>
vector<int> RestoreRoute(int node, int start, int goal) {
    if (node == start) {
        cout << node;
        return;
    }
    RestoreRoute(next_node[start][node], start, goal);
    cout << " " << node;
    if (node == goal) {
        cout << endl;
    }
}
*/

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();

    // RestoreRoute(G, S, G);
    /*
    int u, v;
    while (cin >> u >> v, u | v) {
        cout << "next " << next_node[u][v] << endl;
    }
    */
    int node = S;
    cout << S;
    while (node != G) {
        node = next_node[node][G];
        cout << " " << node;
    }
    cout << endl;

    /*
    cout << cost[S][G] << endl;

    int s, g;
    while (cin >> s >> g, s | g) {
        cout << next_node[s][g] << endl;
    }
    */
}

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