結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー MisterMister
提出日時 2021-06-11 22:40:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 169 ms / 3,000 ms
コード長 1,805 bytes
コンパイル時間 1,420 ms
コンパイル使用メモリ 101,996 KB
最終ジャッジ日時 2025-01-22 06:21:48
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 67
権限があれば一括ダウンロードができます

ソースコード

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