結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー 👑 Kazun
提出日時 2021-05-31 23:55:34
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 298 ms / 3,000 ms
コード長 1,724 bytes
コンパイル時間 1,303 ms
コンパイル使用メモリ 91,096 KB
実行使用メモリ 43,500 KB
最終ジャッジ日時 2024-12-14 21:44:59
合計ジャッジ時間 13,282 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 67
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>

using namespace std;
using ll=long long;
typedef pair<long long, long long> P;
const long long INF = 1e18;
int main() {
    ll N, S, T, K; cin >> N >> S >> T >> K;
    vector<ll> X(N + 1);
    for (int i = 1; i <= N; i++) cin >> X[i];
    ll M; cin >> M;
    vector<vector<P>>E(N + 1);
    ll A, B, Y;
    for (int i = 0; i < M; i++) {
        cin >> A >> B >> Y;
        E[A].push_back({ B, Y });
    }
    vector<vector<ll>> DP(N + 1, vector<ll>(K + 1, INF));
    DP[S][1] = X[S];
    vector<vector<P>>Prev(N + 1, vector<P>(K + 1, { 0, 0 }));
    priority_queue<pair<ll,P>, vector<pair<ll,P>>, greater<pair<ll,P>>>Q;
    Q.push(make_pair(0LL,make_pair(S,1)));
    
    pair<ll,P> R;
    ll d,x,k,y,m;
    while (!Q.empty()) {
        R = Q.top();
        Q.pop();

        d=R.first;
        x=R.second.first;
        k=R.second.second;

        if (DP[x][k] < d) continue;
        if (x == T && k == K) break;
        long long L = min(K, k + 1);
        for (P a : E[x]) {
            y = a.first, m = a.second;
            if (DP[x][k] + m + X[y] < DP[y][L]) {
                DP[y][L] = DP[x][k] + m + X[y];
                Prev[y][L] = { x, k };
                Q.push({DP[y][L],{y,L} });
            }
        }
    }
    if (DP[T][K] >= INF) {
        cout << "Impossible" << endl;
        return 0;
    }

    x = T;
    k = K;

    vector<ll>Z;
    while (k > 0) {
        Z.push_back(x);
        P ans = Prev[x][k];
        x = ans.first; k = ans.second;
    }
    cout << "Possible" << endl << DP[T][K] << endl << Z.size() << endl;
    for (long long i = Z.size() - 1; i >= 0; i--) {
        if (i != Z.size() - 1) cout << ' ';
        cout << Z[i];
    }
    cout << endl;
}
0