結果

問題 No.807 umg tours
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 11:24:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,309 bytes
コンパイル時間 1,437 ms
コンパイル使用メモリ 115,148 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 07:14:35
合計ジャッジ時間 9,100 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 6 ms
4,348 KB
testcase_02 AC 8 ms
4,348 KB
testcase_03 AC 7 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 7 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 TLE -
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>
#include<map>

int main() {
    int n, m;
    std::cin >> n >> m;

    std::map<int, std::map<int, long>> costs;
    int a, b;
    long c;
    for (int i = 0; i < m; i++) {
        std::cin >> a >> b >> c;
        
        a--;
        b--;
        if (costs.count(a) == 0) costs[a] = std::map<int, long>();
        if (costs.count(b) == 0) costs[b] = std::map<int, long>();
        costs[a][b] = c;
        costs[b][a] = c;
    }

    std::vector<long> d1(n, std::numeric_limits<long>::max());
    std::priority_queue<std::tuple<long, int>> q1;
    d1[0] = 0;
    q1.push(std::make_tuple(0, 0));
    while (!q1.empty()) {
        auto [_, u] = q1.top();
        q1.pop();
        
        if (costs.count(u) == 0) {
            continue;
        }

        for (auto&& [v, _] : costs[u]) {
            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<long> d2(n, std::numeric_limits<long>::max()); // NOT SKIPPED
    std::vector<long> d3(n, std::numeric_limits<long>::max()); // SKIPPED
    std::priority_queue<std::tuple<long, 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 [_, u, skipped] = q2.top();
        q2.pop();

        if (costs.count(u) == 0) {
            continue;
        }

        if (skipped) {
            for (auto&& [v, _] : costs[u]) {
                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 (auto&& [v, _] : costs[u]) {
                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