結果

問題 No.2565 はじめてのおつかい
ユーザー a01sa01toa01sa01to
提出日時 2023-12-02 15:38:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,143 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 210,240 KB
実行使用メモリ 9,252 KB
最終ジャッジ日時 2023-12-02 15:38:09
合計ジャッジ時間 6,325 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 87 ms
9,252 KB
testcase_04 AC 66 ms
9,252 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 51 ms
6,676 KB
testcase_07 AC 27 ms
6,676 KB
testcase_08 AC 25 ms
6,676 KB
testcase_09 AC 45 ms
6,676 KB
testcase_10 AC 29 ms
6,676 KB
testcase_11 AC 69 ms
6,676 KB
testcase_12 AC 17 ms
6,676 KB
testcase_13 AC 29 ms
6,676 KB
testcase_14 AC 48 ms
6,676 KB
testcase_15 AC 52 ms
6,676 KB
testcase_16 AC 56 ms
7,552 KB
testcase_17 AC 12 ms
6,676 KB
testcase_18 AC 15 ms
6,676 KB
testcase_19 AC 66 ms
6,676 KB
testcase_20 AC 55 ms
6,676 KB
testcase_21 AC 52 ms
6,676 KB
testcase_22 AC 30 ms
6,676 KB
testcase_23 AC 35 ms
6,676 KB
testcase_24 AC 8 ms
6,676 KB
testcase_25 AC 64 ms
6,676 KB
testcase_26 AC 36 ms
6,676 KB
testcase_27 AC 56 ms
6,676 KB
testcase_28 AC 66 ms
6,676 KB
testcase_29 AC 73 ms
6,928 KB
testcase_30 AC 60 ms
6,676 KB
testcase_31 AC 58 ms
6,676 KB
testcase_32 AC 33 ms
6,676 KB
testcase_33 AC 58 ms
6,676 KB
testcase_34 AC 61 ms
6,676 KB
testcase_35 AC 60 ms
6,984 KB
testcase_36 AC 62 ms
6,676 KB
testcase_37 AC 35 ms
6,676 KB
testcase_38 AC 59 ms
6,676 KB
testcase_39 AC 49 ms
6,676 KB
testcase_40 AC 69 ms
6,872 KB
testcase_41 AC 69 ms
7,168 KB
testcase_42 AC 35 ms
6,676 KB
testcase_43 AC 56 ms
6,676 KB
testcase_44 AC 29 ms
6,676 KB
testcase_45 AC 16 ms
6,676 KB
testcase_46 AC 63 ms
6,676 KB
testcase_47 AC 41 ms
6,676 KB
testcase_48 AC 32 ms
6,676 KB
testcase_49 AC 19 ms
6,676 KB
testcase_50 AC 47 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 43 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
  #define _GLIBCXX_DEBUG
#else
  #define Debug(...) void(0)
#endif
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector Graph(n, vector<int>(0));
  rep(_, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    Graph[a].push_back(b);
  }
  constexpr int INF = 1e8;
  auto bfs = [&](int s, int g) {
    vector<int> dist(n, INF);
    queue<int> q;
    q.push(s);
    dist[s] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int u : Graph[v]) {
        if (dist[u] != INF) continue;
        dist[u] = dist[v] + 1;
        q.push(u);
      }
    }
    return dist[g];
  };
  int d_0n = bfs(0, n - 1);
  int d_n0 = bfs(n - 1, 0);
  int d_0n1 = bfs(0, n - 2);
  int d_n10 = bfs(n - 2, 0);
  int d_nn1 = bfs(n - 1, n - 2);
  int d_n1n = bfs(n - 2, n - 1);
  int ans = INF;
  ans = min(ans, d_0n + d_nn1 + d_n10);
  ans = min(ans, d_0n1 + d_n1n + d_n0);
  Debug(d_0n, d_n0, d_0n1, d_n10, d_nn1, d_n1n);
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
0