結果

問題 No.2565 はじめてのおつかい
ユーザー Peach2587Peach2587
提出日時 2023-12-02 16:44:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 2,488 ms
コンパイル使用メモリ 190,432 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2023-12-02 16:44:41
合計ジャッジ時間 9,005 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 291 ms
16,640 KB
testcase_04 AC 130 ms
16,640 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 78 ms
6,676 KB
testcase_07 AC 42 ms
6,676 KB
testcase_08 AC 43 ms
6,676 KB
testcase_09 AC 71 ms
6,676 KB
testcase_10 AC 54 ms
6,676 KB
testcase_11 AC 153 ms
10,752 KB
testcase_12 AC 24 ms
6,676 KB
testcase_13 AC 49 ms
6,676 KB
testcase_14 AC 93 ms
8,320 KB
testcase_15 AC 96 ms
7,552 KB
testcase_16 AC 88 ms
11,264 KB
testcase_17 AC 17 ms
6,676 KB
testcase_18 AC 22 ms
6,676 KB
testcase_19 AC 117 ms
9,728 KB
testcase_20 AC 128 ms
9,216 KB
testcase_21 AC 94 ms
9,216 KB
testcase_22 AC 49 ms
7,168 KB
testcase_23 AC 60 ms
7,296 KB
testcase_24 AC 10 ms
6,676 KB
testcase_25 AC 108 ms
9,344 KB
testcase_26 AC 58 ms
6,912 KB
testcase_27 AC 94 ms
9,728 KB
testcase_28 AC 115 ms
9,472 KB
testcase_29 AC 139 ms
11,392 KB
testcase_30 AC 113 ms
10,496 KB
testcase_31 AC 113 ms
9,472 KB
testcase_32 AC 55 ms
6,912 KB
testcase_33 AC 111 ms
10,112 KB
testcase_34 AC 114 ms
8,576 KB
testcase_35 AC 112 ms
11,136 KB
testcase_36 AC 113 ms
9,984 KB
testcase_37 AC 57 ms
7,168 KB
testcase_38 AC 101 ms
8,576 KB
testcase_39 AC 86 ms
7,552 KB
testcase_40 AC 125 ms
11,264 KB
testcase_41 AC 133 ms
11,904 KB
testcase_42 AC 59 ms
6,676 KB
testcase_43 AC 91 ms
8,192 KB
testcase_44 AC 45 ms
6,676 KB
testcase_45 AC 23 ms
6,676 KB
testcase_46 AC 109 ms
7,680 KB
testcase_47 AC 57 ms
6,676 KB
testcase_48 AC 52 ms
6,676 KB
testcase_49 AC 23 ms
6,676 KB
testcase_50 AC 73 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 45 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <atcoder/modint>
#include <atcoder/dsu>
#include <boost/rational.hpp>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
const double pi = 3.14159265359879323846264338327950288419;
const ll INF = 9 * 1e6;
using p = pair<ll, ll>;

int main() {
  int n, m; cin >> n >> m;
  map<int, vector<int>> mp;
  for(int i = 0; i < m; i++){
    int U, V; cin >> U >> V;
    mp[U-1].push_back(V-1);
  }
  vector<ll> dis_from_0(n, INF);
  dis_from_0[0] = 0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    int v = q.front(); q.pop();
    for(auto nv : mp[v]){
      if(dis_from_0[nv] != INF) continue;
      dis_from_0[nv] = dis_from_0[v] + 1;
      q.push(nv);
    }
  }
  
  vector<ll> dis_from_n(n, INF);
  dis_from_n[n-1] = 0;
  q.push(n-1);
  while(!q.empty()){
    int v = q.front(); q.pop();
    for(auto nv : mp[v]){
      if(dis_from_n[nv] != INF) continue;
      dis_from_n[nv] = dis_from_n[v] + 1;
      q.push(nv);
    }
  }
  
  vector<ll> dis_from_n1(n, INF);
  dis_from_n1[n-2] = 0;
  q.push(n-2);
  while(!q.empty()){
    int v = q.front(); q.pop();
    for(auto nv : mp[v]){
      if(dis_from_n1[nv] != INF) continue;
      dis_from_n1[nv] = dis_from_n1[v] + 1;
      q.push(nv);
    }
  }
  
  //for(auto c : dis_from_n1) cerr << c << " ";
  ll zero_n_n1 = dis_from_0[n-1] + dis_from_n[n-2] + dis_from_n1[0],
     zero_n1_n = dis_from_0[n-2] + dis_from_n1[n-1] + dis_from_n[0];
  
  cout << (min(zero_n_n1, zero_n1_n) >= INF ? -1:min(zero_n_n1, zero_n1_n)) << endl;
}
0