結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー midri1784midri1784
提出日時 2021-06-11 22:19:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 3,000 ms
コード長 3,397 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 217,660 KB
実行使用メモリ 33,824 KB
最終ジャッジ日時 2023-08-21 12:54:37
合計ジャッジ時間 15,190 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 55 ms
15,876 KB
testcase_05 AC 152 ms
27,764 KB
testcase_06 AC 63 ms
14,108 KB
testcase_07 AC 69 ms
16,844 KB
testcase_08 AC 116 ms
16,504 KB
testcase_09 AC 60 ms
10,656 KB
testcase_10 AC 63 ms
14,040 KB
testcase_11 AC 89 ms
17,720 KB
testcase_12 AC 150 ms
25,696 KB
testcase_13 AC 48 ms
11,348 KB
testcase_14 AC 57 ms
15,408 KB
testcase_15 AC 20 ms
6,160 KB
testcase_16 AC 72 ms
19,880 KB
testcase_17 AC 37 ms
7,760 KB
testcase_18 AC 175 ms
23,344 KB
testcase_19 AC 79 ms
15,080 KB
testcase_20 AC 32 ms
7,352 KB
testcase_21 AC 105 ms
25,576 KB
testcase_22 AC 115 ms
21,704 KB
testcase_23 AC 108 ms
15,404 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 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,376 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 4 ms
4,376 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 7 ms
4,380 KB
testcase_36 AC 6 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 4 ms
4,376 KB
testcase_43 AC 4 ms
4,380 KB
testcase_44 AC 4 ms
4,376 KB
testcase_45 AC 4 ms
5,256 KB
testcase_46 AC 4 ms
4,376 KB
testcase_47 AC 6 ms
5,244 KB
testcase_48 AC 10 ms
8,728 KB
testcase_49 AC 8 ms
6,240 KB
testcase_50 AC 12 ms
8,464 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 14 ms
8,228 KB
testcase_53 AC 8 ms
6,500 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 10 ms
7,580 KB
testcase_56 AC 5 ms
4,376 KB
testcase_57 AC 8 ms
6,268 KB
testcase_58 AC 8 ms
6,088 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 4 ms
4,824 KB
testcase_61 AC 4 ms
4,460 KB
testcase_62 AC 7 ms
5,672 KB
testcase_63 AC 4 ms
4,556 KB
testcase_64 AC 108 ms
24,776 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 81 ms
33,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N, S, T, K; cin >> N >> S >> T >> K;
  --S; --T; 
  vi X(N); RVI(X);
  vector<vector<pi>> edges(N);
  vector<vector<pi>> r_edges(N);
  i64 M; cin >> M;
  REP(0, M, i) {
    i64 a, b, y; RI(a); RI(b); RI(y); --a; --b; 
    edges[a].pb({b, y});
    r_edges[b].pb({a, y});
  }

  auto update = [](const pi &a, const pi &b) {
    if(b.first == -1) {
      return a;
    }
    if(a.first == -1) {
      return b;
    }
    if(b.first < a.first) {
      return b;
    }
    return a;
  };

  // BFS part
  vector<vector<pi>> TS(K, vector<pi>(N, {-1, -1}));
  TS[0][S] = {X[S], -1};
  REP(0, K-1, k) {
    REP(0, N, i) {
      if(TS[k][i].first >= 0) {
        for(auto &e: edges[i]) {
          i64 n = e.first, w = e.second;
          TS[k+1][n] = update(TS[k+1][n], {TS[k][i].first + w + X[n], i});
        }
      }
    }
  }

  // dijkstra part
  vector<pi> D(N, pi(-1, -1));
  using ppi = pair<pi, i64>;
  priority_queue<ppi, vector<ppi>, greater<ppi>> pq;

  D[T] = {0, -1};
  pq.push({D[T], T});
  while(!pq.empty()) {
    auto c = pq.top(); pq.pop();
    i64 cur = c.second;
    pi d = c.first;
    if(d != D[cur]) {
      continue;
    }
    for(auto &e: r_edges[cur]) {
      i64 n = e.first, w = e.second;
      i64 dn = d.first + X[cur] + w;
      if(D[n].first == -1 || dn < D[n].first) {
        D[n] = {dn, cur};
        pq.push({{dn, cur}, n});
      }
    }
  }

  i64 idx = -1, m = -1;
  REP(0, N, i) {
    if(TS[K-1][i].first != -1 && D[i].first != -1) {
      i64 candidate = TS[K-1][i].first + D[i].first;
      if(m == -1 || candidate < m) {
        m = candidate;
        idx = i;
      }
    }
  }

  if(idx == -1) {
    WS("Impossible");
  }
  else {
    vi ans;
    k = K-1;
    i64 prev = idx;
    while(k >= 0) {
      ans.push_back(prev+1);
      prev = TS[k][prev].second;
      --k;
    }
    reverse(all(ans));
    i64 next = D[idx].second;
    while(next != -1) {
      ans.push_back(next+1);
      next = D[next].second;
    }
    //cerr << idx+1 << endl;
    WS("Possible");
    WI(m);
    WI(sz(ans));
    WVI(ans);
  }

  return 0;
}

0