結果

問題 No.2565 はじめてのおつかい
ユーザー gabriel55_gabriel55_
提出日時 2023-12-02 16:02:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,911 bytes
コンパイル時間 4,715 ms
コンパイル使用メモリ 270,816 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2023-12-02 16:02:39
合計ジャッジ時間 7,905 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 54 ms
16,640 KB
testcase_04 AC 37 ms
16,640 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 29 ms
6,656 KB
testcase_07 AC 15 ms
6,548 KB
testcase_08 AC 16 ms
6,548 KB
testcase_09 AC 25 ms
6,548 KB
testcase_10 AC 19 ms
6,548 KB
testcase_11 AC 39 ms
11,392 KB
testcase_12 AC 9 ms
6,548 KB
testcase_13 AC 18 ms
6,548 KB
testcase_14 AC 30 ms
8,704 KB
testcase_15 AC 31 ms
7,936 KB
testcase_16 AC 30 ms
14,464 KB
testcase_17 AC 8 ms
6,548 KB
testcase_18 AC 10 ms
7,936 KB
testcase_19 AC 31 ms
10,240 KB
testcase_20 AC 29 ms
9,600 KB
testcase_21 AC 27 ms
9,728 KB
testcase_22 AC 16 ms
7,680 KB
testcase_23 AC 19 ms
7,680 KB
testcase_24 AC 5 ms
6,548 KB
testcase_25 AC 28 ms
9,856 KB
testcase_26 AC 18 ms
7,168 KB
testcase_27 AC 26 ms
10,368 KB
testcase_28 AC 30 ms
9,856 KB
testcase_29 AC 36 ms
12,160 KB
testcase_30 AC 28 ms
11,264 KB
testcase_31 AC 29 ms
9,984 KB
testcase_32 AC 15 ms
7,296 KB
testcase_33 AC 26 ms
10,752 KB
testcase_34 AC 28 ms
8,960 KB
testcase_35 AC 29 ms
12,672 KB
testcase_36 AC 28 ms
10,752 KB
testcase_37 AC 18 ms
7,424 KB
testcase_38 AC 25 ms
8,960 KB
testcase_39 AC 28 ms
7,936 KB
testcase_40 AC 31 ms
12,032 KB
testcase_41 AC 34 ms
12,928 KB
testcase_42 AC 19 ms
6,912 KB
testcase_43 AC 23 ms
8,576 KB
testcase_44 AC 14 ms
6,548 KB
testcase_45 AC 8 ms
6,548 KB
testcase_46 AC 34 ms
8,192 KB
testcase_47 AC 21 ms
6,548 KB
testcase_48 AC 19 ms
6,548 KB
testcase_49 AC 9 ms
6,548 KB
testcase_50 AC 22 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 16 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if !__INCLUDE_LEVEL__
#include __FILE__

int main(void){
   ll n, m;
   cin >> n >> m;
   vector v(n, vector<ll>());
   rep(i,m) {
      ll a, b;
      cin >> a >> b;
      a--; b--;
      v[a].emplace_back(b);
   }
   vector<P> dist(n, {inf, 0}), distnn1(n, {inf, 0}), distn1n(n, {inf, 0});
   dist[0] = {0, 1};
   auto bfs = [&](ll st, vector<P> &y) -> void {
      queue<ll> que;
      que.emplace(st);
      while(!que.empty()) {
         ll x = que.front();
         que.pop();
         for(auto &nx : v[x]) {
            if(y[nx].fi < y[x].fi + 1) continue;
            else if(y[nx].fi == y[x].fi + 1) {
               y[nx].se += y[x].se;
               continue;
            }
            y[nx].fi = y[x].fi + 1;
            y[nx].se = y[x].se;
            que.emplace(nx);
         }
      }
   };
   bfs(0, dist);
   distnn1[n-1] = dist[n-1];
   distn1n[n-2] = dist[n-2];
   bfs(n-1, distnn1);
   bfs(n-2, distn1n);
   vector<P> distn1(n, {inf, 0}), distn11(n, {inf, 0});
   distn1[n-1] = distn1n[n-1];
   distn11[n-2] = distnn1[n-2];
   bfs(n-1, distn1);
   bfs(n-2, distn11);
   debug(dist, distnn1, distn1n, distn1, distn11);
   if(distn1[0].fi == distn11[0].fi) {
      if(distn1[0].fi == inf) {
         cout << -1 << '\n';
         return 0;
      }
      cout << distn1[0].fi << '\n';
      return 0;
   }
   if(distn1[0].fi < distn11[0].fi) {
      swap(distn1[0], distn11[0]);
   }
   cout << distn11[0].fi << '\n';
}

/*---------------------------------------------------------------------------------------------------
      ○______
       ||      |
       ||   ●   |
       ||      |
        || ̄ ̄ ̄ ̄ ̄
       ||  君が代は
   ∧__,,∧||  千代に八千代に
  ( `・ω・||    さざれ石の巌となりて
   ヽ  つ0     こけのむすまで
   し―-J
---------------------------------------------------------------------------------------------------*/

#else
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using P = pair<ll, ll>;
#define fi first
#define se second
#define m_99(i,a,b) for (ll i = a, i##_range = (b); i < i##_range; i++)
#define REP(i,a,b) m_99(i,a,b)
#define rep(i,a) m_99(i,0,a)
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }
#ifdef _DEBUG
#include <debug_print.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif
struct initialise { initialise() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; }__INI__;
const ll inf = 1LL << 60;
const ll dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
#endif
0