結果

問題 No.807 umg tours
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 10:58:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,396 bytes
コンパイル時間 1,142 ms
コンパイル使用メモリ 98,988 KB
実行使用メモリ 813,612 KB
最終ジャッジ日時 2023-10-17 07:13:56
合計ジャッジ時間 4,248 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
#include<limits>

typedef unsigned long long ull;

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<ull>> costs(n, std::vector<ull>(n, -1));
    for (int i = 0; i < m; i++) {
        int a, b;
        ull c;
        std::cin >> a >> b >> c;

        a--;
        b--;
        costs[a][b] = c;
        costs[b][a] = c;
    }

    std::vector<ull> d1(n, std::numeric_limits<ull>::max());
    std::priority_queue<std::tuple<ull, int>> q1;
    d1[0] = 0;
    q1.push(std::make_tuple(0, 0));
    while (!q1.empty()) {
        auto t = q1.top();
        q1.pop();

        int u = std::get<1>(t);
        for (int v = 0; v < n; v++) {
            if (costs[u][v] == -1) {
                continue;
            }

            if (d1[u] + costs[u][v] < d1[v]) {
                d1[v] = d1[u] + costs[u][v];
                q1.push(std::make_tuple(d1[v], v));
            }
        }
    }

    std::vector<ull> d2(n, std::numeric_limits<ull>::max()); // NOT SKIPPED
    std::vector<ull> d3(n, std::numeric_limits<ull>::max()); // SKIPPED
    std::priority_queue<std::tuple<ull, int, bool>> q2;
    d2[0] = 0;
    d3[0] = 0;
    q2.push(std::make_tuple(0, 0, false));
    q2.push(std::make_tuple(0, 0, true));

    while (!q2.empty()) {
        auto t = q2.top();
        q2.pop();

        int u = std::get<1>(t);
        bool skipped = std::get<2>(t);
        if (skipped) {
            for (int v = 0; v < n; v++) {
                if (costs[u][v] == -1) {
                    continue;
                }

                if (d3[u] + costs[u][v] < d3[v]) {
                    d3[v] = d3[u] + costs[u][v];
                    q2.push(std::make_tuple(d3[v], v, true));
                }
            }
        } else {
            for (int v = 0; v < n; v++) {
                if (costs[u][v] == -1) {
                    continue;
                }

                if (d2[u] + costs[u][v] < d2[v]) {
                    d2[v] = d2[u] + costs[u][v];
                    q2.push(std::make_tuple(d2[v], v, false));
                }

                if (d2[u] < d3[v]) {
                    d3[v] = d2[u];
                    q2.push(std::make_tuple(d3[v], v, true));
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        std::cout << d1[i] + d3[i] << std::endl;
    }
}
0