結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー homer12homer12
提出日時 2021-06-14 22:32:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 3,000 ms
コード長 2,950 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 220,488 KB
実行使用メモリ 25,972 KB
最終ジャッジ日時 2023-08-26 08:52:44
合計ジャッジ時間 17,371 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 39 ms
11,884 KB
testcase_05 AC 93 ms
19,692 KB
testcase_06 AC 46 ms
10,484 KB
testcase_07 AC 53 ms
11,880 KB
testcase_08 AC 88 ms
13,728 KB
testcase_09 AC 44 ms
7,020 KB
testcase_10 AC 45 ms
9,048 KB
testcase_11 AC 53 ms
12,520 KB
testcase_12 AC 79 ms
17,844 KB
testcase_13 AC 30 ms
7,988 KB
testcase_14 AC 34 ms
12,136 KB
testcase_15 AC 14 ms
4,888 KB
testcase_16 AC 44 ms
14,972 KB
testcase_17 AC 28 ms
5,352 KB
testcase_18 AC 110 ms
13,324 KB
testcase_19 AC 52 ms
10,684 KB
testcase_20 AC 27 ms
5,988 KB
testcase_21 AC 59 ms
19,000 KB
testcase_22 AC 63 ms
14,340 KB
testcase_23 AC 72 ms
9,532 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 4 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 5 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 6 ms
4,380 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 10 ms
4,380 KB
testcase_36 AC 9 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 5 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,376 KB
testcase_42 AC 4 ms
4,376 KB
testcase_43 AC 5 ms
4,376 KB
testcase_44 AC 3 ms
4,376 KB
testcase_45 AC 3 ms
4,740 KB
testcase_46 AC 5 ms
4,376 KB
testcase_47 AC 8 ms
4,608 KB
testcase_48 AC 12 ms
7,584 KB
testcase_49 AC 12 ms
5,252 KB
testcase_50 AC 31 ms
7,220 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 30 ms
7,176 KB
testcase_53 AC 10 ms
5,396 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 19 ms
6,480 KB
testcase_56 AC 3 ms
4,380 KB
testcase_57 AC 22 ms
5,788 KB
testcase_58 AC 12 ms
5,224 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 4 ms
4,376 KB
testcase_61 AC 6 ms
4,376 KB
testcase_62 AC 13 ms
4,996 KB
testcase_63 AC 9 ms
4,380 KB
testcase_64 AC 149 ms
23,472 KB
testcase_65 AC 5 ms
4,380 KB
testcase_66 AC 57 ms
25,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}
template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
    for(auto &x:arr) {cout << x << " ";} cout << endl;
}

// =====================================================>

/*

*/


ll n,m,s,t,k;
lvector x;
vector<vector<lPair>> g;

void solve() {
    // 爲了方便計算,頂點從0開始
    // 輸出的時候再加1
    cin>>n>>s>>t>>k; --s; --t;
    x = lvector(n+1);
    range(i, 0, n) cin>>x[i];
    cin>>m;
    g = vector<vector<lPair>>(n+1);
    while(m--) {
        ll a,b,y; cin>>a>>b>>y; --a; --b;
        g[a].emplace_back(b, y);
    }

    // 包含端點要有 K 個節點
    // 0, 1, 2, ..., K-1 層
    // 除開 K-1 層外,其他層都向上建邊
    // 頂點編號 [0, n), [n, 2n), ..., [(k-1)n, kn)
    lvector dist(n*k, LINF);
    lvector prev(n*k, -1);
    using T = tuple<ll, ll, ll>;
    priority_queue<T, vector<T>, greater<T>> pq;
    dist[s] = x[s];
    pq.emplace(dist[s], s, -1);
    while(pq.size()) {
        auto [d, node, p] = pq.top(); pq.pop();
        if(d > dist[node]) continue;
        prev[node] = p;
        if(node == (k-1)*n+t) break;
        // 鬆弛鄰點
        ll layer = node / n;
        ll u = node % n;
        for(auto [v,w]: g[u]) {
            ll next_node = v + (layer + (layer!=k-1)) * n;
            if(chmin(dist[next_node], d+w+x[v])) {
                pq.emplace(d+w+x[v], next_node, node);
            }
        }
    }

    if(dist[(k-1)*n+t] == LINF) cout << "Impossible" << endl;
    else {
        cout << "Possible" << endl;
        cout << dist[(k-1)*n+t] << endl;
        lvector path; ll node = (k-1)*n+t;
        while(node != -1) {
            path.push_back(node);
            node = prev[node];
        }
        cout << path.size() << endl;
        for(auto it=path.rbegin(); it!=path.rend(); ++it) {
            cout << (*it)%n+1 << " ";
        }
        cout << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
0