結果

問題 No.2565 はじめてのおつかい
ユーザー takumi152takumi152
提出日時 2023-12-02 15:41:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,370 bytes
コンパイル時間 1,245 ms
コンパイル使用メモリ 117,208 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2023-12-02 15:41:26
合計ジャッジ時間 3,991 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 31 ms
8,832 KB
testcase_04 AC 27 ms
8,832 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 24 ms
6,676 KB
testcase_07 AC 13 ms
6,676 KB
testcase_08 AC 12 ms
6,676 KB
testcase_09 AC 22 ms
6,676 KB
testcase_10 AC 14 ms
6,676 KB
testcase_11 AC 33 ms
6,676 KB
testcase_12 AC 9 ms
6,676 KB
testcase_13 AC 14 ms
6,676 KB
testcase_14 AC 24 ms
6,676 KB
testcase_15 AC 26 ms
6,676 KB
testcase_16 AC 23 ms
7,168 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 8 ms
6,676 KB
testcase_19 AC 28 ms
6,676 KB
testcase_20 AC 25 ms
6,676 KB
testcase_21 AC 22 ms
6,676 KB
testcase_22 AC 12 ms
6,676 KB
testcase_23 AC 15 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 22 ms
6,676 KB
testcase_26 AC 15 ms
6,676 KB
testcase_27 AC 20 ms
6,676 KB
testcase_28 AC 25 ms
6,676 KB
testcase_29 AC 27 ms
6,676 KB
testcase_30 AC 21 ms
6,676 KB
testcase_31 AC 23 ms
6,676 KB
testcase_32 AC 12 ms
6,676 KB
testcase_33 AC 20 ms
6,676 KB
testcase_34 AC 24 ms
6,676 KB
testcase_35 AC 22 ms
6,784 KB
testcase_36 AC 23 ms
6,676 KB
testcase_37 AC 14 ms
6,676 KB
testcase_38 AC 21 ms
6,676 KB
testcase_39 AC 23 ms
6,676 KB
testcase_40 AC 26 ms
6,676 KB
testcase_41 AC 28 ms
6,912 KB
testcase_42 AC 16 ms
6,676 KB
testcase_43 AC 20 ms
6,676 KB
testcase_44 AC 12 ms
6,676 KB
testcase_45 AC 6 ms
6,676 KB
testcase_46 AC 28 ms
6,676 KB
testcase_47 AC 19 ms
6,676 KB
testcase_48 AC 16 ms
6,676 KB
testcase_49 AC 10 ms
6,676 KB
testcase_50 AC 23 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 16 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 998244353;

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> edge(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    edge[u-1].push_back(v-1);
  }

  int ans = -1;
  vector<vector<bool>> visited(4, vector<bool>(n));
  deque<tuple<int, int, int>> que;
  que.emplace_back(0, 0, 0);
  while (!que.empty()) {
    auto [dist, now_task, now_id] = que.front();
    que.pop_front();
    if (visited[now_task][now_id]) continue;
    visited[now_task][now_id] = true;
    if (now_task == 3 && now_id == 0) {
      ans = dist;
      break;
    }
    if (now_id == n - 2 && (now_task & 1) == 0) {
      que.emplace_front(dist, now_task | 1, now_id);
      continue;
    }
    if (now_id == n - 1 && (now_task & 2) == 0) {
      que.emplace_front(dist, now_task | 2, now_id);
      continue;
    }
    for (auto &next_id: edge[now_id]) {
      if (!visited[now_task][next_id]) {
        que.emplace_back(dist + 1, now_task, next_id);
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0