結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー ぷらぷら
提出日時 2021-06-11 21:50:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 3,000 ms
コード長 2,448 bytes
コンパイル時間 2,229 ms
コンパイル使用メモリ 218,260 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2024-05-08 17:29:55
合計ジャッジ時間 13,800 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 104 ms
13,696 KB
testcase_05 AC 277 ms
19,968 KB
testcase_06 AC 114 ms
11,648 KB
testcase_07 AC 139 ms
14,976 KB
testcase_08 AC 236 ms
12,952 KB
testcase_09 AC 118 ms
7,168 KB
testcase_10 AC 131 ms
14,976 KB
testcase_11 AC 159 ms
13,696 KB
testcase_12 AC 242 ms
19,072 KB
testcase_13 AC 85 ms
9,728 KB
testcase_14 AC 90 ms
12,416 KB
testcase_15 AC 34 ms
6,940 KB
testcase_16 AC 118 ms
15,104 KB
testcase_17 AC 76 ms
6,940 KB
testcase_18 AC 317 ms
20,864 KB
testcase_19 AC 150 ms
11,776 KB
testcase_20 AC 67 ms
6,016 KB
testcase_21 AC 168 ms
19,968 KB
testcase_22 AC 178 ms
20,352 KB
testcase_23 AC 204 ms
10,240 KB
testcase_24 AC 10 ms
6,944 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 9 ms
5,376 KB
testcase_29 AC 10 ms
5,376 KB
testcase_30 AC 7 ms
5,376 KB
testcase_31 AC 6 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 8 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 14 ms
5,376 KB
testcase_36 AC 12 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 7 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 5 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 6 ms
5,376 KB
testcase_43 AC 9 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 5 ms
5,376 KB
testcase_47 AC 8 ms
5,376 KB
testcase_48 AC 10 ms
5,504 KB
testcase_49 AC 11 ms
5,376 KB
testcase_50 AC 26 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 26 ms
5,376 KB
testcase_53 AC 9 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 18 ms
5,376 KB
testcase_56 AC 4 ms
5,376 KB
testcase_57 AC 19 ms
5,376 KB
testcase_58 AC 11 ms
5,376 KB
testcase_59 AC 3 ms
5,376 KB
testcase_60 AC 4 ms
5,376 KB
testcase_61 AC 6 ms
5,376 KB
testcase_62 AC 11 ms
5,376 KB
testcase_63 AC 9 ms
5,376 KB
testcase_64 AC 262 ms
20,604 KB
testcase_65 AC 5 ms
6,944 KB
testcase_66 AC 128 ms
26,880 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