結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー SSRSSSRS
提出日時 2021-06-11 21:36:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 3,000 ms
コード長 2,215 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 191,384 KB
実行使用メモリ 28,336 KB
最終ジャッジ日時 2024-05-08 17:08:11
合計ジャッジ時間 14,652 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 118 ms
13,140 KB
testcase_05 AC 305 ms
21,564 KB
testcase_06 AC 120 ms
11,580 KB
testcase_07 AC 143 ms
13,984 KB
testcase_08 AC 233 ms
12,056 KB
testcase_09 AC 130 ms
8,416 KB
testcase_10 AC 127 ms
12,344 KB
testcase_11 AC 169 ms
14,200 KB
testcase_12 AC 286 ms
19,696 KB
testcase_13 AC 93 ms
9,428 KB
testcase_14 AC 102 ms
12,736 KB
testcase_15 AC 36 ms
6,016 KB
testcase_16 AC 134 ms
15,956 KB
testcase_17 AC 83 ms
6,844 KB
testcase_18 AC 329 ms
18,592 KB
testcase_19 AC 155 ms
12,092 KB
testcase_20 AC 70 ms
5,888 KB
testcase_21 AC 199 ms
20,656 KB
testcase_22 AC 213 ms
18,372 KB
testcase_23 AC 235 ms
10,900 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 8 ms
5,376 KB
testcase_29 AC 9 ms
5,376 KB
testcase_30 AC 6 ms
5,376 KB
testcase_31 AC 5 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 4 ms
5,376 KB
testcase_35 AC 12 ms
5,376 KB
testcase_36 AC 9 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 5 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 5 ms
5,376 KB
testcase_43 AC 7 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 4 ms
5,376 KB
testcase_46 AC 4 ms
5,376 KB
testcase_47 AC 6 ms
5,376 KB
testcase_48 AC 11 ms
8,320 KB
testcase_49 AC 8 ms
6,144 KB
testcase_50 AC 11 ms
8,704 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 12 ms
8,576 KB
testcase_53 AC 9 ms
6,400 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 11 ms
7,808 KB
testcase_56 AC 5 ms
5,376 KB
testcase_57 AC 7 ms
6,016 KB
testcase_58 AC 8 ms
6,272 KB
testcase_59 AC 3 ms
5,376 KB
testcase_60 AC 5 ms
5,376 KB
testcase_61 AC 4 ms
5,376 KB
testcase_62 AC 7 ms
5,632 KB
testcase_63 AC 4 ms
5,376 KB
testcase_64 AC 215 ms
19,536 KB
testcase_65 AC 3 ms
5,376 KB
testcase_66 AC 132 ms
28,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
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>>> E(N), E2(N);
  for (int i = 0; i < M; i++){
    int A, B, Y;
    cin >> A >> B >> Y;
    A--;
    B--;
    E[A].push_back(make_pair(Y, B));
    E2[B].push_back(make_pair(Y, A));
  }
  vector<vector<long long>> dp(K, vector<long long>(N, INF));
  vector<vector<int>> pr1(K, vector<int>(N, -1));
  dp[0][S] = X[S];
  for (int i = 0; i < K - 1; i++){
    for (int j = 0; j < N; j++){
      for (auto P : E[j]){
        int w = P.second;
        long long sum = dp[i][j] + P.first + X[w];
        if (dp[i + 1][w] > sum){
          pr1[i + 1][w] = j;
          dp[i + 1][w] = sum;
        }
      }
    }
  }
  vector<long long> d(N, INF);
  vector<int> pr2(N);
  priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<tuple<long long, int, int>>> pq;
  pq.push(make_tuple(0, T, -1));
  while (!pq.empty()){
    long long dd = get<0>(pq.top());
    int v = get<1>(pq.top());
    int p = get<2>(pq.top());
    pq.pop();
    if (d[v] == INF){
      d[v] = dd;
      pr2[v] = p;
      for (auto P : E2[v]){
        int w = P.second;
        if (d[w] == INF){
          pq.push(make_tuple(dd + X[v] + P.first, w, v));
        }
      }
    }
  }
  long long mn = INF, t = -1;
  for (int i = 0; i < N; i++){
    long long sum = dp[K - 1][i] + d[i];
    if (sum < mn){
      mn = sum;
      t = i;
    }
  }
  if (t == -1){
    cout << "Impossible" << endl;
  } else {
    vector<int> ans;
    int t1 = t;
    ans.push_back(t1);
    for (int i = K - 1; i >= 1; i--){
      int tmp = pr1[i][t1];
      ans.push_back(tmp);
      t1 = tmp;
    }
    reverse(ans.begin(), ans.end());
    int t2 = t;
    while (t2 != T){
      t2 = pr2[t2];
      ans.push_back(t2);
    }
    cout << "Possible" << endl;
    cout << mn << endl;
    int R = ans.size();
    cout << R << endl;
    for (int i = 0; i < R; i++){
      cout << ans[i] + 1;
      if (i < R - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}
0