結果

問題 No.2565 はじめてのおつかい
ユーザー KudeKude
提出日時 2023-12-12 23:10:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 2,098 bytes
コンパイル時間 3,391 ms
コンパイル使用メモリ 278,480 KB
実行使用メモリ 26,644 KB
最終ジャッジ日時 2023-12-12 23:10:46
合計ジャッジ時間 10,968 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 163 ms
26,644 KB
testcase_04 AC 62 ms
26,644 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 53 ms
10,368 KB
testcase_07 AC 22 ms
6,912 KB
testcase_08 AC 24 ms
7,936 KB
testcase_09 AC 44 ms
9,344 KB
testcase_10 AC 30 ms
8,576 KB
testcase_11 AC 86 ms
16,984 KB
testcase_12 AC 13 ms
6,676 KB
testcase_13 AC 30 ms
8,192 KB
testcase_14 AC 60 ms
12,776 KB
testcase_15 AC 59 ms
11,904 KB
testcase_16 AC 72 ms
20,132 KB
testcase_17 AC 12 ms
6,676 KB
testcase_18 AC 17 ms
9,556 KB
testcase_19 AC 104 ms
15,356 KB
testcase_20 AC 65 ms
14,272 KB
testcase_21 AC 58 ms
14,364 KB
testcase_22 AC 34 ms
10,496 KB
testcase_23 AC 39 ms
10,624 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 69 ms
14,524 KB
testcase_26 AC 40 ms
9,984 KB
testcase_27 AC 66 ms
15,216 KB
testcase_28 AC 75 ms
14,984 KB
testcase_29 AC 90 ms
18,172 KB
testcase_30 AC 70 ms
16,500 KB
testcase_31 AC 67 ms
14,776 KB
testcase_32 AC 35 ms
9,984 KB
testcase_33 AC 69 ms
15,780 KB
testcase_34 AC 63 ms
13,548 KB
testcase_35 AC 75 ms
18,212 KB
testcase_36 AC 76 ms
15,744 KB
testcase_37 AC 38 ms
10,368 KB
testcase_38 AC 65 ms
13,444 KB
testcase_39 AC 59 ms
11,648 KB
testcase_40 AC 86 ms
17,956 KB
testcase_41 AC 92 ms
19,216 KB
testcase_42 AC 40 ms
9,728 KB
testcase_43 AC 57 ms
12,648 KB
testcase_44 AC 26 ms
8,064 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 73 ms
12,544 KB
testcase_47 AC 36 ms
8,320 KB
testcase_48 AC 31 ms
8,320 KB
testcase_49 AC 13 ms
6,676 KB
testcase_50 AC 38 ms
8,704 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 21 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr int INF = 1001001001;
template <int k = 2, class T>  // cost in [0, k)
vector<int> kBFS(const vector<vector<pair<int, T>>>& g, int s = 0) {
  static_assert(k >= 1);
  const int n = g.size();
  vector<int> dist(n, INF);
  vector<int> q[2 * k];
  dist[s] = 0;
  q[0].emplace_back(s);
  for (int dist_cur = 0, ptr = 0;;) {
    const auto qb = q + ptr;
    while (!qb[0].empty()) {
      int u = qb[0].back();
      qb[0].pop_back();
      if (dist[u] != dist_cur) continue;
      for (auto [v, c] : g[u]) {
        int nd = dist_cur + c;
        if (nd < dist[v]) {
          dist[v] = nd;
          qb[c].emplace_back(v);
        }
      }
    }
    do {
      dist_cur++;
      ptr++;
      if (ptr == 2 * k) return dist;
    } while (q[ptr].empty());
    if (ptr > k) {
      for (int i = ptr; i < 2 * k; i++) {
        swap(q[i], q[i - ptr]);
      }
      ptr = 0;
    }
  }
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<P>> to(4 * n);
  rep(_, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    rep(s, 4) to[n * s + u].emplace_back(n * s + v, 1);
  }
  rep(s0, 2) to[n * s0 + n - 2].emplace_back(n * (s0 + 2) + n - 2, 0);
  rep(s1, 2) to[n * (2 * s1) + n - 1].emplace_back(n * (2 * s1 + 1) + n - 1, 0);
  int ans = kBFS(to)[3 * n];
  if (ans == INF) ans = -1;
  cout << ans << '\n';
}
0