結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー MisterMister
提出日時 2021-06-11 22:40:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 3,000 ms
コード長 1,805 bytes
コンパイル時間 1,260 ms
コンパイル使用メモリ 107,148 KB
実行使用メモリ 33,536 KB
最終ジャッジ日時 2024-05-08 18:45:52
合計ジャッジ時間 11,499 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 47 ms
16,896 KB
testcase_05 AC 155 ms
24,596 KB
testcase_06 AC 58 ms
13,824 KB
testcase_07 AC 70 ms
17,152 KB
testcase_08 AC 119 ms
13,884 KB
testcase_09 AC 44 ms
7,168 KB
testcase_10 AC 55 ms
16,512 KB
testcase_11 AC 77 ms
16,128 KB
testcase_12 AC 129 ms
23,680 KB
testcase_13 AC 43 ms
11,136 KB
testcase_14 AC 45 ms
15,872 KB
testcase_15 AC 17 ms
6,144 KB
testcase_16 AC 62 ms
19,456 KB
testcase_17 AC 28 ms
5,376 KB
testcase_18 AC 161 ms
22,528 KB
testcase_19 AC 73 ms
13,440 KB
testcase_20 AC 29 ms
5,888 KB
testcase_21 AC 78 ms
25,472 KB
testcase_22 AC 80 ms
23,552 KB
testcase_23 AC 86 ms
10,624 KB
testcase_24 AC 5 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 6 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 5 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 6 ms
5,376 KB
testcase_34 AC 5 ms
5,376 KB
testcase_35 AC 10 ms
5,376 KB
testcase_36 AC 9 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 5 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 4 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 5 ms
5,376 KB
testcase_43 AC 5 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 5 ms
5,376 KB
testcase_47 AC 7 ms
5,376 KB
testcase_48 AC 11 ms
7,616 KB
testcase_49 AC 11 ms
5,376 KB
testcase_50 AC 27 ms
7,332 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 28 ms
7,168 KB
testcase_53 AC 9 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 18 ms
6,632 KB
testcase_56 AC 3 ms
5,376 KB
testcase_57 AC 20 ms
5,632 KB
testcase_58 AC 10 ms
5,376 KB
testcase_59 AC 4 ms
5,376 KB
testcase_60 AC 4 ms
5,376 KB
testcase_61 AC 6 ms
5,376 KB
testcase_62 AC 12 ms
5,376 KB
testcase_63 AC 8 ms
5,376 KB
testcase_64 AC 164 ms
24,724 KB
testcase_65 AC 5 ms
5,376 KB
testcase_66 AC 77 ms
33,536 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