結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー abc3abc3
提出日時 2021-06-12 06:01:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 3,000 ms
コード長 2,477 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 221,780 KB
実行使用メモリ 27,160 KB
最終ジャッジ日時 2023-08-22 01:47:31
合計ジャッジ時間 19,249 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 42 ms
12,660 KB
testcase_05 AC 134 ms
21,008 KB
testcase_06 AC 49 ms
10,792 KB
testcase_07 AC 59 ms
12,832 KB
testcase_08 AC 110 ms
12,780 KB
testcase_09 AC 42 ms
7,200 KB
testcase_10 AC 46 ms
9,872 KB
testcase_11 AC 68 ms
13,368 KB
testcase_12 AC 110 ms
18,900 KB
testcase_13 AC 36 ms
8,676 KB
testcase_14 AC 42 ms
12,516 KB
testcase_15 AC 14 ms
5,368 KB
testcase_16 AC 52 ms
16,000 KB
testcase_17 AC 27 ms
5,664 KB
testcase_18 AC 133 ms
15,008 KB
testcase_19 AC 65 ms
11,152 KB
testcase_20 AC 27 ms
5,860 KB
testcase_21 AC 69 ms
20,200 KB
testcase_22 AC 70 ms
16,016 KB
testcase_23 AC 82 ms
9,900 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 5 ms
4,376 KB
testcase_29 AC 5 ms
4,376 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 5 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 6 ms
4,376 KB
testcase_34 AC 5 ms
4,380 KB
testcase_35 AC 9 ms
4,376 KB
testcase_36 AC 8 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 5 ms
4,380 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 5 ms
4,376 KB
testcase_43 AC 5 ms
4,376 KB
testcase_44 AC 6 ms
5,316 KB
testcase_45 AC 5 ms
5,612 KB
testcase_46 AC 7 ms
4,756 KB
testcase_47 AC 10 ms
6,172 KB
testcase_48 AC 17 ms
10,368 KB
testcase_49 AC 16 ms
7,340 KB
testcase_50 AC 33 ms
10,688 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 33 ms
10,428 KB
testcase_53 AC 15 ms
7,824 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 25 ms
9,768 KB
testcase_56 AC 7 ms
5,604 KB
testcase_57 AC 22 ms
7,228 KB
testcase_58 AC 15 ms
7,516 KB
testcase_59 AC 5 ms
4,500 KB
testcase_60 AC 6 ms
5,456 KB
testcase_61 AC 7 ms
4,512 KB
testcase_62 AC 15 ms
6,712 KB
testcase_63 AC 10 ms
5,148 KB
testcase_64 AC 138 ms
22,416 KB
testcase_65 AC 4 ms
4,380 KB
testcase_66 AC 62 ms
27,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    using P=pair<ll,ll>;
    const ll INF=1e18;
    const int mod=998244353;
    
    struct edge{
        int to;
        ll w;
        edge(int to,ll w):to(to),w(w){}
    };
    
    void solve(){
        int n,s,t,k,m;
        cin>>n>>s>>t>>k;
        s--;t--;
        vector<int>x(n);
        rep(i,n){cin>>x[i];}
        cin>>m;
        vector<vector<edge>>G(n);
        rep(i,m){
            int a,b,y;
            cin>>a>>b>>y;a--;b--;
            G[a].emplace_back(b,x[b]+y);
        }

        vector<vector<ll>>dist(k+1,vector<ll>(n,INF));
        vector<vector<int>>prev(k+1,vector<int>(n,-1));
        vector<vector<int>>prec(k+1,vector<int>(n,-1));
        
        priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>>q;
        q.push(make_pair(x[s],make_pair(0,s)));
        dist[0][s]=x[s];

        while(!q.empty()){
            int c=q.top().second.first;
            int v=q.top().second.second;
            ll cost=q.top().first;
            q.pop();
            if(cost!=dist[c][v]){continue;}
            for(auto u:G[v]){
                int nc=min(k-1,c+1);
                if(chmin(dist[nc][u.to],cost+u.w)){
                    prev[nc][u.to]=v;
                    prec[nc][u.to]=c;
                    q.push(make_pair(dist[nc][u.to],make_pair(nc,u.to)));
                }
            }
        }

        if(dist[k-1][t]==INF){
            cout<<"Impossible"<<endl;
            return;
        }
        cout<<"Possible"<<endl;
        cout<<dist[k-1][t]<<endl;
        vector<int>ans;
        int p=t,pc=k-1;
        while(pc!=-1){
            ans.push_back(p);
            int np=pc;
            pc=prec[np][p];
            p=prev[np][p];
        }
        reverse(ans.begin(),ans.end());
        cout<<ans.size()<<endl;
        
        rep(i,ans.size()){
            printf("%d%c",ans[i]+1,(i+1<ans.size()?' ':'\n'));
        }
    }
    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        solve();
        return 0;
    }
0