結果

問題 No.2569 はじめてのおつかいHard
ユーザー eve__fuyukieve__fuyuki
提出日時 2023-12-08 15:55:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 377 ms / 2,000 ms
コード長 1,589 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 214,288 KB
実行使用メモリ 26,380 KB
最終ジャッジ日時 2023-12-08 15:55:41
合計ジャッジ時間 6,197 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
9,856 KB
testcase_01 AC 52 ms
9,856 KB
testcase_02 AC 51 ms
9,856 KB
testcase_03 AC 52 ms
9,856 KB
testcase_04 AC 52 ms
9,856 KB
testcase_05 AC 377 ms
25,828 KB
testcase_06 AC 179 ms
16,392 KB
testcase_07 AC 354 ms
26,100 KB
testcase_08 AC 377 ms
26,380 KB
testcase_09 AC 98 ms
13,416 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

const long long INF = 1e18;
vector<long long> dist(int s, vector<vector<pair<int, long long>>> &g) {
    vector<long long> d(g.size(), INF);
    d[s] = 0;
    priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                   greater<>>
        q;
    q.push({0, s});
    while (!q.empty()) {
        auto [d_v, v] = q.top();
        q.pop();
        if (d_v != d[v]) {
            continue;
        }
        for (auto [to, w] : g[v]) {
            if (d[to] > d[v] + w) {
                d[to] = d[v] + w;
                q.push({d[to], to});
            }
        }
    }
    return d;
}
int main() {
    fast_io();
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, long long>>> g(n), g_rev(n);
    for (int i = 0; i < m; i++) {
        int u, v, t;
        cin >> u >> v >> t;
        u--;
        v--;
        g[u].push_back({v, t});
        g_rev[v].push_back({u, t});
    }
    auto dist_1 = dist(n - 2, g);
    auto dist_2 = dist(n - 1, g);
    auto dist_1_rev = dist(n - 2, g_rev);
    auto dist_2_rev = dist(n - 1, g_rev);
    for (int k = 0; k < n - 2; k++) {
        long long ans = INF;
        // k to n - 2 to n - 1 to k
        ans = min(ans, dist_1_rev[k] + dist_1[n - 1] + dist_2[k]);
        // k to n - 1 to n - 2 to k
        ans = min(ans, dist_2_rev[k] + dist_2[n - 2] + dist_1[k]);
        if (ans == INF) {
            cout << -1 << endl;
        } else {
            cout << ans << endl;
        }
    }
}
0