結果

問題 No.2565 はじめてのおつかい
ユーザー Preds1dentPreds1dent
提出日時 2023-12-02 16:01:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,094 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 111,460 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2023-12-02 16:01:24
合計ジャッジ時間 5,223 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 68 ms
11,136 KB
testcase_04 AC 62 ms
11,136 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 45 ms
6,676 KB
testcase_07 AC 26 ms
6,676 KB
testcase_08 AC 25 ms
6,676 KB
testcase_09 AC 45 ms
6,676 KB
testcase_10 AC 28 ms
6,676 KB
testcase_11 AC 60 ms
7,808 KB
testcase_12 AC 17 ms
6,676 KB
testcase_13 AC 27 ms
6,676 KB
testcase_14 AC 43 ms
6,676 KB
testcase_15 AC 46 ms
6,676 KB
testcase_16 AC 54 ms
9,344 KB
testcase_17 AC 12 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 63 ms
7,040 KB
testcase_20 AC 52 ms
6,784 KB
testcase_21 AC 48 ms
6,912 KB
testcase_22 AC 29 ms
6,676 KB
testcase_23 AC 33 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 50 ms
6,912 KB
testcase_26 AC 34 ms
6,676 KB
testcase_27 AC 51 ms
7,296 KB
testcase_28 AC 59 ms
6,912 KB
testcase_29 AC 64 ms
8,192 KB
testcase_30 AC 59 ms
7,680 KB
testcase_31 AC 53 ms
7,040 KB
testcase_32 AC 33 ms
6,676 KB
testcase_33 AC 56 ms
7,424 KB
testcase_34 AC 55 ms
6,676 KB
testcase_35 AC 54 ms
8,448 KB
testcase_36 AC 53 ms
7,424 KB
testcase_37 AC 30 ms
6,676 KB
testcase_38 AC 54 ms
6,676 KB
testcase_39 AC 42 ms
6,676 KB
testcase_40 AC 59 ms
8,192 KB
testcase_41 AC 63 ms
8,576 KB
testcase_42 AC 34 ms
6,676 KB
testcase_43 AC 49 ms
6,676 KB
testcase_44 AC 25 ms
6,676 KB
testcase_45 AC 14 ms
6,676 KB
testcase_46 AC 54 ms
6,676 KB
testcase_47 AC 39 ms
6,676 KB
testcase_48 AC 30 ms
6,676 KB
testcase_49 AC 18 ms
6,676 KB
testcase_50 AC 45 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 39 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
using namespace std;
int main() {
  int n, m; cin >> n >> m;
  vector to(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    u--; v--;
    to[u].push_back(v);
  }
  const long long INF = 1001001001;
  auto bfs = [&](int s) -> vector<long long> {
    vector<long long> dp(n, INF);
    queue<int> q;
    q.push(s);
    dp[s] = 0;
    while (q.size()) {
      auto v = q.front(); q.pop();
      for (auto u : to[v]) {
        if (dp[u] == INF) {
          dp[u] = dp[v] + 1;
          q.push(u);
        }
      }
    }
    return dp;
  };
  auto dp0 = bfs(0);
  // for (int i = 0; i < n; i++) {
  //   cout << (dp0[i] == INF ? -1ll : dp0[i]) << (i + 1 == n ? "\n" : " ");
  // }
  auto dpnp = bfs(n - 2);
  auto dpn = bfs(n - 1);
  long long ans = min({dp0[n - 2] + dpnp[n - 1] + dpn[0], dp0[n - 2] + dpnp[0] + dp0[n - 1] + dpn[0], dp0[n - 1] + dpn[n - 2] + dpnp[0]});
  if (ans >= INF) ans = -1;
  cout << ans << endl;

}
0