結果

問題 No.2565 はじめてのおつかい
ユーザー Nichi10pNichi10p
提出日時 2023-12-02 15:40:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 984 bytes
コンパイル時間 2,174 ms
コンパイル使用メモリ 209,972 KB
実行使用メモリ 26,748 KB
最終ジャッジ日時 2023-12-02 15:40:48
合計ジャッジ時間 6,866 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 136 ms
26,748 KB
testcase_04 AC 92 ms
26,748 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 61 ms
8,704 KB
testcase_07 AC 35 ms
6,548 KB
testcase_08 AC 34 ms
7,552 KB
testcase_09 AC 55 ms
7,808 KB
testcase_10 AC 37 ms
7,936 KB
testcase_11 AC 88 ms
16,192 KB
testcase_12 AC 20 ms
6,548 KB
testcase_13 AC 36 ms
7,296 KB
testcase_14 AC 60 ms
11,856 KB
testcase_15 AC 66 ms
10,624 KB
testcase_16 AC 77 ms
19,980 KB
testcase_17 AC 15 ms
6,548 KB
testcase_18 AC 22 ms
9,660 KB
testcase_19 AC 86 ms
14,308 KB
testcase_20 AC 79 ms
13,352 KB
testcase_21 AC 73 ms
13,572 KB
testcase_22 AC 39 ms
10,240 KB
testcase_23 AC 48 ms
10,112 KB
testcase_24 AC 10 ms
6,548 KB
testcase_25 AC 69 ms
13,732 KB
testcase_26 AC 44 ms
9,472 KB
testcase_27 AC 75 ms
14,552 KB
testcase_28 AC 80 ms
13,808 KB
testcase_29 AC 93 ms
17,508 KB
testcase_30 AC 81 ms
15,836 KB
testcase_31 AC 76 ms
13,984 KB
testcase_32 AC 42 ms
9,600 KB
testcase_33 AC 74 ms
15,244 KB
testcase_34 AC 74 ms
12,372 KB
testcase_35 AC 82 ms
17,804 KB
testcase_36 AC 100 ms
15,080 KB
testcase_37 AC 44 ms
9,856 KB
testcase_38 AC 70 ms
12,524 KB
testcase_39 AC 60 ms
10,752 KB
testcase_40 AC 95 ms
17,292 KB
testcase_41 AC 93 ms
18,552 KB
testcase_42 AC 48 ms
8,960 KB
testcase_43 AC 65 ms
11,728 KB
testcase_44 AC 31 ms
7,552 KB
testcase_45 AC 17 ms
6,548 KB
testcase_46 AC 73 ms
11,008 KB
testcase_47 AC 50 ms
6,912 KB
testcase_48 AC 39 ms
7,424 KB
testcase_49 AC 21 ms
6,548 KB
testcase_50 AC 58 ms
6,784 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 43 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template<class T> bool chmin(T &a, const T &x) { return x < a ? a = x, true : false; }

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> G(4*N + 1);
  while (M--) {
    int u, v;
    std::cin >> u >> v;
    if (v < N-1) {
      for (int s = 0; s < (1 << 2); ++s)
        G[s*N + u].push_back(s*N + v);
    }
    if (v == N-1) {
      G[0b00*N + u].push_back(0b01*N + v);
      G[0b10*N + u].push_back(0b11*N + v);
    }
    if (v == N) {
      G[0b00*N + u].push_back(0b10*N + v);
      G[0b01*N + u].push_back(0b11*N + v);
    }
  }

  constexpr int inf = 1000000000;
  std::vector<int> D(4*N + 1, inf);
  std::queue<int> bfs;
  D[1] = 0;
  bfs.push(1);
  while (!bfs.empty()) {
    const int u = bfs.front();
    bfs.pop();
    for (const int &v : G[u])
      if (D[v] == inf)
        D[v] = D[u] + 1, bfs.push(v);
  }

  if (D[0b11*N + 1] < inf)
    std::cout << D[0b11*N + 1] << '\n';
  else
    std::cout << "-1" << '\n';
}
0