結果

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

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

const unsigned long long INF = 1002003004005006007;
template<class T>
std::pair<std::vector<unsigned long long>, std::vector<int>> dijkstra(std::vector<std::vector<std::pair<int,T>>>& to, int s=0) {
    struct QueElem {
        int v;
        unsigned long long c;
        bool operator<(const QueElem a) const {return c > a.c;}
        QueElem(int v, unsigned long long c): v(v), c(c) {}
    };
    std::pair<std::vector<unsigned long long>, std::vector<int>> res;
    std::vector<unsigned long long>& dist = res.first;
    std::vector<int>& pre = res.second;
    dist.resize(to.size(), INF);
    pre.resize(to.size(), -1);
    std::priority_queue<QueElem> q;
    dist[s] = 0;
    pre[s] = -1;
    q.emplace(s, 0);
    while(!q.empty()) {
        QueElem qe = q.top(); q.pop();
        int u = qe.v;
        unsigned long long c = qe.c;
        if (c > dist[u]) continue;
        for(auto vc: to[u]) {
            int v = vc.first;
            unsigned long long nc = c + vc.second;
            if (nc < dist[v]) {
                dist[v] = nc;
                pre[v] = u;
                q.emplace(v, nc);
            }
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, s, t, k;
    cin >> n >> s >> t >> k;
    s--, t--; k--;
    VI x(n);
    rep(i, n) cin >> x[i];
    int m;
    cin >> m;
    vector<vector<P>> to(n * (k + 1));
    rep(_, m) {
        int a, b, y;
        cin >> a >> b >> y;
        --a, --b;
        rep(j, k) {
            to[(k + 1) * a + j].emplace_back((k + 1) * b + j + 1, y + x[b]);
        }
        to[(k + 1) * a + k].emplace_back((k + 1) * b + k, y + x[b]);
    }
    auto [dist, pre] = dijkstra(to, (k + 1) * s);
    if (dist[(k + 1) * t + k] == INF) {
        cout << "Impossible" << '\n';
        return 0;
    }
    cout << "Possible" << '\n';
    cout << x[s] + dist[(k + 1) * t + k] << '\n';
    VI ps;
    for(int v = (k + 1) * t + k; v != -1; v = pre[v]) {
        ps.push_back(v / (k + 1));
    }
    int sz = ps.size();
    cout << sz << '\n';
    rrep(i, sz) cout << ps[i] + 1 << " \n"[i == 0];
}
0