結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー ぷらぷら
提出日時 2021-06-11 21:50:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 3,000 ms
コード長 2,448 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 215,716 KB
実行使用メモリ 26,740 KB
最終ジャッジ日時 2023-08-21 12:11:15
合計ジャッジ時間 16,395 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 101 ms
13,296 KB
testcase_05 AC 277 ms
19,764 KB
testcase_06 AC 113 ms
11,452 KB
testcase_07 AC 142 ms
14,676 KB
testcase_08 AC 223 ms
12,640 KB
testcase_09 AC 113 ms
7,092 KB
testcase_10 AC 121 ms
14,940 KB
testcase_11 AC 146 ms
13,296 KB
testcase_12 AC 234 ms
18,844 KB
testcase_13 AC 83 ms
9,692 KB
testcase_14 AC 88 ms
12,404 KB
testcase_15 AC 32 ms
5,648 KB
testcase_16 AC 118 ms
15,044 KB
testcase_17 AC 72 ms
5,652 KB
testcase_18 AC 291 ms
20,628 KB
testcase_19 AC 135 ms
11,624 KB
testcase_20 AC 65 ms
5,676 KB
testcase_21 AC 159 ms
19,804 KB
testcase_22 AC 173 ms
20,084 KB
testcase_23 AC 195 ms
10,104 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 9 ms
4,376 KB
testcase_29 AC 9 ms
4,380 KB
testcase_30 AC 7 ms
4,376 KB
testcase_31 AC 7 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 8 ms
4,376 KB
testcase_34 AC 6 ms
4,380 KB
testcase_35 AC 14 ms
4,376 KB
testcase_36 AC 11 ms
4,376 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 7 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 6 ms
4,376 KB
testcase_43 AC 7 ms
4,376 KB
testcase_44 AC 3 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 5 ms
4,376 KB
testcase_47 AC 7 ms
4,380 KB
testcase_48 AC 9 ms
5,348 KB
testcase_49 AC 11 ms
4,384 KB
testcase_50 AC 26 ms
5,116 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 26 ms
5,296 KB
testcase_53 AC 9 ms
4,508 KB
testcase_54 AC 3 ms
4,376 KB
testcase_55 AC 17 ms
4,960 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 18 ms
4,420 KB
testcase_58 AC 10 ms
4,380 KB
testcase_59 AC 3 ms
4,376 KB
testcase_60 AC 3 ms
4,380 KB
testcase_61 AC 5 ms
4,376 KB
testcase_62 AC 11 ms
4,376 KB
testcase_63 AC 8 ms
4,376 KB
testcase_64 AC 256 ms
21,520 KB
testcase_65 AC 4 ms
4,380 KB
testcase_66 AC 125 ms
26,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lii = pair<long long,pair<int,int>>;

const long long inf = 1001001001001001001;

bool chmin(long long &x,long long y) {
    if(x > y) {
        x = y;
        return true;
    }
    return false;
}

int main() {
    int N,S,T,K;
    cin >> N >> S >> T >> K;
    S--;
    T--;
    vector<int>X(N);
    for(int i = 0; i < N; i++) {
        cin >> X[i];
    }
    int M;
    cin >> M;
    vector<vector<pair<int,int>>>road(N),road2(N);
    for(int i = 0; i < M; i++) {
        int A,B,Y;
        cin >> A >> B >> Y;
        A--;
        B--;
        road[A].push_back({B,Y});
        road2[B].push_back({A,Y});
    }
    vector<vector<long long>>dist(N,vector<long long>(K+1,inf));
    dist[S][1] = X[S];
    priority_queue<lii,vector<lii>,greater<lii>>que;
    que.push({X[S],{S,1}});
    while (!que.empty()) {
        lii x = que.top();
        que.pop();
        long long cost = x.first;
        int n1 = x.second.first;
        int n2 = x.second.second;
        if(dist[n1][n2] != cost) {
            continue;
        }
        n2 = min(K,n2+1);
        for(int i = 0; i < road[n1].size(); i++) {
            int nxt = road[n1][i].first;
            long long cost2 = cost+road[n1][i].second+X[nxt];
            if(chmin(dist[nxt][n2],cost2)) {
                que.push({cost2,{nxt,n2}});
            }
        }
    }
    if(dist[T][K] == inf) {
        cout << "Impossible" << endl;
    }
    else {
        cout << "Possible" << endl;
        cout << dist[T][K] << endl;
        int n1 = T,n2 = K;
        vector<int>ans;
        ans.push_back(T+1);
        while (!(n1 == S && n2 == 1)) {
            for(int i = 0; i < road2[n1].size(); i++) {
                int nxt = road2[n1][i].first;
                long long cost = road2[n1][i].second+X[n1];
                if(dist[nxt][n2-1]+cost == dist[n1][n2]) {
                    ans.push_back(nxt+1);
                    n1 = nxt;
                    n2 = n2-1;
                    break;
                }
                if(dist[nxt][n2]+cost == dist[n1][n2]) {
                    ans.push_back(nxt+1);
                    n1 = nxt;
                    n2 = n2;
                    break;
                }
            }
        }
        reverse(ans.begin(),ans.end());
        cout << ans.size() << endl;
        for(int i = 0; i < ans.size(); i++) {
            cout << ans[i] << ((i+1 == ans.size())?"\n":" ");
        }
    }
}
0