結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー MisterMister
提出日時 2021-06-11 22:40:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 3,000 ms
コード長 1,805 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 106,868 KB
実行使用メモリ 33,528 KB
最終ジャッジ日時 2023-08-21 13:24:24
合計ジャッジ時間 14,529 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 47 ms
16,780 KB
testcase_05 AC 144 ms
24,564 KB
testcase_06 AC 55 ms
13,744 KB
testcase_07 AC 68 ms
17,272 KB
testcase_08 AC 115 ms
13,760 KB
testcase_09 AC 44 ms
7,184 KB
testcase_10 AC 54 ms
16,716 KB
testcase_11 AC 78 ms
16,168 KB
testcase_12 AC 129 ms
23,460 KB
testcase_13 AC 40 ms
11,096 KB
testcase_14 AC 44 ms
15,820 KB
testcase_15 AC 16 ms
5,904 KB
testcase_16 AC 59 ms
19,428 KB
testcase_17 AC 29 ms
5,444 KB
testcase_18 AC 148 ms
22,444 KB
testcase_19 AC 70 ms
13,292 KB
testcase_20 AC 29 ms
5,636 KB
testcase_21 AC 79 ms
25,288 KB
testcase_22 AC 79 ms
23,432 KB
testcase_23 AC 82 ms
10,360 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 4 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 5 ms
4,376 KB
testcase_29 AC 6 ms
4,376 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 5 ms
4,376 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 5 ms
4,376 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 10 ms
4,380 KB
testcase_36 AC 8 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 6 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 5 ms
4,376 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 3 ms
4,696 KB
testcase_46 AC 5 ms
4,380 KB
testcase_47 AC 7 ms
4,496 KB
testcase_48 AC 10 ms
7,240 KB
testcase_49 AC 11 ms
5,112 KB
testcase_50 AC 27 ms
7,208 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 27 ms
7,268 KB
testcase_53 AC 9 ms
5,228 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 17 ms
6,696 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 20 ms
5,480 KB
testcase_58 AC 10 ms
5,212 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 4 ms
4,380 KB
testcase_61 AC 5 ms
4,376 KB
testcase_62 AC 11 ms
4,848 KB
testcase_63 AC 8 ms
4,380 KB
testcase_64 AC 151 ms
25,900 KB
testcase_65 AC 5 ms
4,380 KB
testcase_66 AC 75 ms
33,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

using namespace std;
using lint = long long;

constexpr lint INF = 1LL << 60;

void solve() {
    int n, s, t, k;
    cin >> n >> s >> t >> k;
    --s, --t;

    vector<lint> xs(n);
    for (auto& x : xs) cin >> x;

    vector<vector<pair<int, lint>>> graph(n);
    {
        int m;
        cin >> m;
        while (m--) {
            int u, v;
            lint c;
            cin >> u >> v >> c;
            graph[--u].emplace_back(--v, c);
        }
    }

    auto dp = vector(n, vector(k + 1, INF));
    auto rev = vector(n, vector(k + 1, make_pair(-1, -1)));
    dp[s][1] = xs[s];
    rev[s][1] = {0, 0};

    MinHeap<tuple<lint, int, int>> heap;
    heap.emplace(dp[s][1], s, 1);
    while (!heap.empty()) {
        auto [d, v, p] = heap.top();
        heap.pop();
        if (dp[v][p] < d) continue;

        auto np = min(p + 1, k);
        for (auto [u, c] : graph[v]) {
            auto nd = d + c + xs[u];
            if (dp[u][np] <= nd) continue;

            dp[u][np] = nd;
            rev[u][np] = {v, p};
            heap.emplace(dp[u][np], u, np);
        }
    }

    if (dp[t][k] == INF) {
        cout << "Impossible\n";
        return;
    }

    cout << "Possible\n"
         << dp[t][k] << "\n";

    vector<int> ans;
    {
        int v = t, p = k;
        while (p > 0) {
            ans.push_back(v);
            tie(v, p) = rev[v][p];
        }
        reverse(ans.begin(), ans.end());
    }

    cout << ans.size() << "\n";
    for (auto v : ans) cout << v + 1 << " ";
    cout << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0