結果

問題 No.2565 はじめてのおつかい
ユーザー LanatusLanatus
提出日時 2024-01-09 21:32:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 6,869 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 147,108 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-09-27 20:03:03
合計ジャッジ時間 6,272 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 77 ms
9,856 KB
testcase_04 AC 65 ms
9,856 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 54 ms
6,940 KB
testcase_07 AC 27 ms
6,940 KB
testcase_08 AC 29 ms
6,944 KB
testcase_09 AC 47 ms
6,940 KB
testcase_10 AC 33 ms
6,940 KB
testcase_11 AC 75 ms
7,040 KB
testcase_12 AC 18 ms
6,940 KB
testcase_13 AC 32 ms
6,944 KB
testcase_14 AC 53 ms
6,940 KB
testcase_15 AC 59 ms
6,944 KB
testcase_16 AC 54 ms
8,064 KB
testcase_17 AC 11 ms
6,940 KB
testcase_18 AC 15 ms
6,944 KB
testcase_19 AC 67 ms
6,940 KB
testcase_20 AC 58 ms
6,940 KB
testcase_21 AC 52 ms
6,940 KB
testcase_22 AC 29 ms
6,940 KB
testcase_23 AC 35 ms
6,944 KB
testcase_24 AC 6 ms
6,940 KB
testcase_25 AC 59 ms
6,944 KB
testcase_26 AC 37 ms
6,944 KB
testcase_27 AC 51 ms
6,940 KB
testcase_28 AC 64 ms
6,944 KB
testcase_29 AC 69 ms
7,296 KB
testcase_30 AC 56 ms
6,940 KB
testcase_31 AC 59 ms
6,944 KB
testcase_32 AC 34 ms
6,940 KB
testcase_33 AC 57 ms
6,940 KB
testcase_34 AC 62 ms
6,944 KB
testcase_35 AC 54 ms
7,552 KB
testcase_36 AC 60 ms
6,944 KB
testcase_37 AC 34 ms
6,940 KB
testcase_38 AC 59 ms
6,940 KB
testcase_39 AC 51 ms
6,944 KB
testcase_40 AC 64 ms
7,296 KB
testcase_41 AC 68 ms
7,808 KB
testcase_42 AC 37 ms
6,944 KB
testcase_43 AC 54 ms
6,944 KB
testcase_44 AC 29 ms
6,944 KB
testcase_45 AC 14 ms
6,944 KB
testcase_46 AC 60 ms
6,944 KB
testcase_47 AC 40 ms
6,944 KB
testcase_48 AC 33 ms
6,940 KB
testcase_49 AC 18 ms
6,944 KB
testcase_50 AC 46 ms
6,944 KB
testcase_51 AC 2 ms
6,940 KB
testcase_52 AC 38 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int MOD = 1e9 + 7;

using ld = long double;
using ll = long long;
using ull = unsigned long long;

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> auto min(const T &a) { *min_elemenet(all(a)); }
template <class T> auto max(const T &a) { *max_elemenet(all(a)); }

// const ll MOD = 1e9 + 7;
const ll INF = 1 << 30;
const ll INFLL = 1LL << 60;
const ld EPS = 1e-9;

const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

#define all(v) begin(v), end(v)

struct mint {
  ull a;

  constexpr mint(const ull x = 0) noexcept : a(x % MOD) {}
  constexpr mint operator+(const mint rhs) const noexcept {
    return mint(*this) += rhs;
  }
  constexpr mint operator-(const mint rhs) const noexcept {
    return mint(*this) -= rhs;
  }
  constexpr mint operator*(const mint rhs) const noexcept {
    return mint(*this) *= rhs;
  }
  constexpr mint operator/(const mint rhs) const noexcept {
    return mint(*this) /= rhs;
  }
  constexpr mint &operator+=(const mint rhs) noexcept {
    a += rhs.a;
    if (a >= MOD)
      a -= MOD;
    return *this;
  }
  constexpr mint &operator-=(const mint rhs) noexcept {
    if (a < rhs.a)
      a += MOD;
    a -= rhs.a;
    return *this;
  }
  constexpr mint &operator*=(const mint rhs) noexcept {
    a = a * rhs.a % MOD;
    return *this;
  }
  constexpr mint &operator/=(mint rhs) noexcept {
    ull exp = MOD - 2;
    while (exp) {
      if (exp % 2)
        *this *= rhs;
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
  constexpr mint pow(const mint &a, ull n) noexcept {
    if (n <= 0)
      return 1;
    auto t = pow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

long long modpow(long long a, long long n, long long m = MOD) {
  if (n <= 0)
    return 1LL;
  if (n % 2LL)
    return a * modpow(a, n - 1) % MOD;
  long long t = modpow(a, n / 2);
  return t % MOD * t % MOD;
}

struct Combination {
  const int MAX = 2e5 + 5;
  vector<long long> fact, invfact;

  Combination() {
    fact.resize(MAX);
    invfact.resize(MAX);

    fact[0] = 1;

    for (int i = 1; i < MAX; ++i) {
      fact[i] = fact[i - 1] * i % MOD;
    }

    invfact[MAX - 1] = modpow(fact[MAX - 1], MOD - 2);

    for (int i = MAX - 1; i > 0; --i) {
      invfact[i - 1] = invfact[i] * i % MOD;
    }
  }

  long long nCr(int n, int r) {
    long long a = fact[n];
    long long b = invfact[r] * invfact[n - r] % MOD;

    return a * b % MOD;
  }
};

struct UnionFind {
  vector<int> size, parents;
  UnionFind() {}
  UnionFind(int n) { // make n trees.
    size.resize(n, 0);
    parents.resize(n, 0);
    for (int i = 0; i < n; i++) {
      makeTree(i);
    }
  }
  void makeTree(int x) {
    parents[x] = x; // the parent of x is x
    size[x] = 1;
  }
  bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
  bool unite(int x, int y) {
    x = findRoot(x);
    y = findRoot(y);
    if (x == y)
      return false;
    if (size[x] > size[y]) {
      parents[y] = x;
      size[x] += size[y];
    } else {
      parents[x] = y;
      size[y] += size[x];
    }
    return true;
  }
  int findRoot(int x) {
    if (x != parents[x]) {
      parents[x] = findRoot(parents[x]);
    }
    return parents[x];
  }
  int treeSize(int x) { return size[findRoot(x)]; }
};

// struct UnionFind {
//   int n;
//   vector<int> parent, siz;

//   UnionFind(int n) : n(n) {
//     parent.resize(n);
//     siz.resize(n);

//     for (int i = 0; i < n; ++i) {
//       parent[i] = i;
//       siz[i] = 1;
//     }
//   }

//   int root(int x) {
//     if (parent[x] == x)
//       return x;
//     return parent[x] = root(parent[x]);
//   }

//   bool unite(int x, int y) {
//     x = root(x);
//     y = root(y);

//     if (x == y)
//       return false;

//     if (siz[x] < siz[y])
//       swap(x, y);
//     parent[y] = x;
//     siz[x] += siz[y];

//     return true;
//   }

//   int same(int x, int y) { return root(x) == root(y); }

//   int size(int x) { return siz[root(x)]; }
// };

struct RollingHash {
  int n;
  vector<ull> HS;
  vector<ull> mp;

  const ull b = 3491;

  RollingHash(string &S) {
    n = S.size();
    mp.assign(n, 0);
    HS.assign(n + 1, 0);

    for (int i = 0; i < n; ++i) {
      mp[i] = modpow(b, i);
    }

    for (int i = 0; i < n; ++i) {
      HS[i + 1] = HS[i] + mp[n - i - 1] * (S[i] - '0') % MOD;
      HS[i + 1] %= MOD;
    }
  }

  int getHash(int l, int r) {
    if (r > n)
      return -1;
    return (HS[r] - HS[l] + MOD) % MOD;
  }
  int getHash(string &T) {
    if (T.size() > n)
      return -1;
    int m = T.size();
    int ret = 0;

    for (int i = 0; i < m; ++i) {
      ret += mp[m - i - 1] * (T[i] - '0') % MOD;
      ret %= MOD;
    }

    return ret;
  }

  bool isSubstr(string &T) {
    int m = T.size();
    if (m > n)
      return false;

    int ht = getHash(T);
    vector<int> u;

    for (int i = n - m; i >= 0; --i) {
      int hs = getHash(i, i + m);
      if (hs == ht)
        u.push_back(i);

      ht *= b;
      ht %= MOD;
    }

    for (int i = 0; i < u.size(); ++i) {
      cout << u[u.size() - i - 1] << endl;
    }

    return false;
  }
};

int main(void) {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> G(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;

    G[a - 1].push_back(b - 1);
  }

  auto dijkstra = [&](int s) {
    vector<int> dp(n, INF);
    pqg<pair<int,int>> que;

    dp[s] = 0;
    que.push({0, s});

    while (!que.empty()) {
      auto [d, v] = que.top();
      que.pop();

      if (dp[v] != d) continue;

      for (int to : G[v]) {
        if (chmin(dp[to], d + 1)) {
          que.push({d + 1, to});
        }
      }
    }

    return dp;
  };

  vector<int> dp1 = dijkstra(0);
  ll startToN = dp1[n - 1];
  ll startToM = dp1[n - 2];

  vector<int> dp2 = dijkstra(n - 1);
  vector<int> dp3 = dijkstra(n - 2);

  ll ans = min({
    startToN + dp2[n - 2] + dp3[0],
    startToM + dp3[n - 1] + dp2[0]
  });

  cout << (ans >= INF ? -1 : ans) << endl;
  return 0;
}
0