結果

問題 No.2565 はじめてのおつかい
ユーザー yansi819yansi819
提出日時 2024-04-05 09:13:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 1,088 bytes
コンパイル時間 4,447 ms
コンパイル使用メモリ 267,492 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-05 09:13:19
合計ジャッジ時間 11,655 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 98 ms
11,136 KB
testcase_04 AC 72 ms
11,136 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 54 ms
6,676 KB
testcase_07 AC 29 ms
6,676 KB
testcase_08 AC 28 ms
6,676 KB
testcase_09 AC 49 ms
6,676 KB
testcase_10 AC 32 ms
6,676 KB
testcase_11 AC 78 ms
7,808 KB
testcase_12 AC 19 ms
6,676 KB
testcase_13 AC 32 ms
6,676 KB
testcase_14 AC 54 ms
6,676 KB
testcase_15 AC 57 ms
6,676 KB
testcase_16 AC 62 ms
9,344 KB
testcase_17 AC 14 ms
6,676 KB
testcase_18 AC 18 ms
6,676 KB
testcase_19 AC 71 ms
7,168 KB
testcase_20 AC 61 ms
6,912 KB
testcase_21 AC 59 ms
6,912 KB
testcase_22 AC 35 ms
6,676 KB
testcase_23 AC 38 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 63 ms
7,040 KB
testcase_26 AC 38 ms
6,676 KB
testcase_27 AC 60 ms
7,296 KB
testcase_28 AC 70 ms
7,040 KB
testcase_29 AC 77 ms
8,320 KB
testcase_30 AC 64 ms
7,680 KB
testcase_31 AC 66 ms
7,040 KB
testcase_32 AC 35 ms
6,676 KB
testcase_33 AC 63 ms
7,424 KB
testcase_34 AC 64 ms
6,676 KB
testcase_35 AC 65 ms
8,448 KB
testcase_36 AC 64 ms
7,424 KB
testcase_37 AC 36 ms
6,676 KB
testcase_38 AC 65 ms
6,676 KB
testcase_39 AC 51 ms
6,676 KB
testcase_40 AC 75 ms
8,192 KB
testcase_41 AC 78 ms
8,704 KB
testcase_42 AC 39 ms
6,676 KB
testcase_43 AC 57 ms
6,676 KB
testcase_44 AC 29 ms
6,676 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 63 ms
6,676 KB
testcase_47 AC 42 ms
6,676 KB
testcase_48 AC 32 ms
6,676 KB
testcase_49 AC 19 ms
6,676 KB
testcase_50 AC 51 ms
6,676 KB
testcase_51 AC 3 ms
6,676 KB
testcase_52 AC 43 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using mint = modint998244353;

const ll INF = 1LL << 60;

vector<ll> bfs(int start, vector<vector<int>> &graph, int N) {
  vector<ll> dists(N, INF);
  dists[start] = 0;

  queue<pair<int, ll>> q;
  q.push({start, 0});
  while (!q.empty()) {
    auto [u, d] = q.front(); q.pop();
    for (int v: graph[u]) {
      if (dists[v] < INF) continue;
      dists[v] = d + 1;
      q.push({v, dists[v]});
    }
  }
  return dists;
} 

int main() {
  int N, M; cin >> N >> M;
  vector<vector<int>> graph(N);
  for (int i = 0; i < M; i++) {
    int u, v; cin >> u >> v;
    u--, v--;
    graph[u].push_back(v);
  }
  vector<ll> dists_0 = bfs(0, graph, N);
  vector<ll> dists_N_2 = bfs(N - 2, graph, N);
  vector<ll> dists_N_1 = bfs(N - 1, graph, N);

  ll ans1 = dists_0[N - 2] + dists_N_2[N - 1] + dists_N_1[0];
  ll ans2 = dists_0[N - 1] + dists_N_1[N - 2] + dists_N_2[0];
  ll ans = min(ans1, ans2); 
  cout << (ans >= INF ? -1: ans) << endl;
  return 0;
}
0