結果
問題 | No.2565 はじめてのおつかい |
ユーザー | komori3 |
提出日時 | 2023-12-02 15:07:03 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 8,937 bytes |
コンパイル時間 | 4,931 ms |
コンパイル使用メモリ | 275,320 KB |
実行使用メモリ | 9,856 KB |
最終ジャッジ日時 | 2024-09-26 18:06:17 |
合計ジャッジ時間 | 7,728 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 45 ms
9,856 KB |
testcase_04 | AC | 34 ms
9,856 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 26 ms
5,376 KB |
testcase_07 | AC | 13 ms
5,376 KB |
testcase_08 | AC | 14 ms
5,376 KB |
testcase_09 | AC | 23 ms
5,376 KB |
testcase_10 | AC | 16 ms
5,376 KB |
testcase_11 | AC | 38 ms
6,912 KB |
testcase_12 | AC | 10 ms
5,376 KB |
testcase_13 | AC | 16 ms
5,376 KB |
testcase_14 | AC | 26 ms
5,632 KB |
testcase_15 | AC | 27 ms
5,504 KB |
testcase_16 | AC | 25 ms
8,064 KB |
testcase_17 | AC | 7 ms
5,376 KB |
testcase_18 | AC | 9 ms
5,376 KB |
testcase_19 | AC | 30 ms
6,528 KB |
testcase_20 | AC | 28 ms
6,272 KB |
testcase_21 | AC | 26 ms
6,144 KB |
testcase_22 | AC | 16 ms
5,376 KB |
testcase_23 | AC | 19 ms
5,376 KB |
testcase_24 | AC | 4 ms
5,376 KB |
testcase_25 | AC | 31 ms
6,272 KB |
testcase_26 | AC | 18 ms
5,376 KB |
testcase_27 | AC | 25 ms
6,528 KB |
testcase_28 | AC | 31 ms
6,272 KB |
testcase_29 | AC | 34 ms
7,424 KB |
testcase_30 | AC | 28 ms
6,912 KB |
testcase_31 | AC | 28 ms
6,272 KB |
testcase_32 | AC | 16 ms
5,376 KB |
testcase_33 | AC | 28 ms
6,784 KB |
testcase_34 | AC | 31 ms
5,888 KB |
testcase_35 | AC | 27 ms
7,424 KB |
testcase_36 | AC | 30 ms
6,784 KB |
testcase_37 | AC | 18 ms
5,376 KB |
testcase_38 | AC | 30 ms
5,888 KB |
testcase_39 | AC | 26 ms
5,504 KB |
testcase_40 | AC | 31 ms
7,296 KB |
testcase_41 | AC | 33 ms
7,680 KB |
testcase_42 | AC | 18 ms
5,376 KB |
testcase_43 | AC | 26 ms
5,632 KB |
testcase_44 | AC | 14 ms
5,376 KB |
testcase_45 | AC | 8 ms
5,376 KB |
testcase_46 | AC | 30 ms
5,376 KB |
testcase_47 | AC | 19 ms
5,376 KB |
testcase_48 | AC | 16 ms
5,376 KB |
testcase_49 | AC | 9 ms
5,376 KB |
testcase_50 | AC | 22 ms
5,376 KB |
testcase_51 | AC | 2 ms
5,376 KB |
testcase_52 | AC | 18 ms
5,376 KB |
ソースコード
#define _CRT_NONSTDC_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #define ENABLE_DUMP //#define ENABLE_PERF #include <bits/stdc++.h> #include <random> #include <unordered_set> #include <atcoder/all> //#include <json.hpp> //#include <boost/rational.hpp> //#include <boost/multiprecision/cpp_int.hpp> //using u128 = boost::multiprecision::uint128_t; #ifdef _MSC_VER #include <conio.h> #include <ppl.h> #include <filesystem> //#include <opencv2/core.hpp> //#include <opencv2/highgui.hpp> //#include <opencv2/imgproc.hpp> #include <intrin.h> int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } #else #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif /** compro_io **/ #ifdef ATCODER_MODINT_HPP using mint = atcoder::modint998244353; std::ostream& operator<<(std::ostream& os, const mint& m) { os << m.val(); return os; } std::istream& operator>>(std::istream& is, mint& m) { uint64_t x; is >> x; m = x; return is; } #endif namespace aux { template<typename T, unsigned N, unsigned L> struct tp { static void output(std::ostream& os, const T& v) { os << std::get<N>(v) << ", "; tp<T, N + 1, L>::output(os, v); } }; template<typename T, unsigned N> struct tp<T, N, N> { static void output(std::ostream& os, const T& v) { os << std::get<N>(v); } }; } template<typename... Ts> std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) { os << '['; aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t); return os << ']'; } // tuple out template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x); // container out (fwd decl) template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) { return os << "[" << p.first << ", " << p.second << "]"; } // pair out template<class S, class T> std::istream& operator>>(std::istream& is, std::pair<S, T>& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector<bool>::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector<bool>& v) { bool f = true; os << "["; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << "]"; return os; } // vector<bool> out template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x) { bool f = true; os << "["; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << "]"; } // container out template<class T, class = decltype(std::begin(std::declval<T&>())), class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template<typename T> auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template<typename... Ts> std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector<char> b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #ifdef ENABLE_DUMP #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<</*__PRETTY_FUNCTION__*/__FUNCTION__<<"]"<<std::endl;DUMPBUF<<" ";dump_func(__VA_ARGS__);DUMPOUT<<DUMPBUF.str();DUMPBUF.str("");DUMPBUF.clear();}while(0); void dump_func() { DUMPBUF << std::endl; } template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } #else #define DUMP(...) void(0); #endif /* timer */ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 3.0e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 3.0e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } } timer; /** perf counter **/ #if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__) #define __PRETTY_FUNCTION__ __FUNCSIG__ #endif struct PerfCounter { std::string name; Timer timer; PerfCounter(const std::string& name_) : name(name_) {} ~PerfCounter() { #ifdef ENABLE_PERF std::cerr << format("[PerfCounter] %6d ms <%s>.\n", (int)timer.elapsed_ms(), name.c_str()); #endif } }; /** rand **/ struct Xorshift { static constexpr uint64_t M = INT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); } inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); } inline int next_int() { return next() & M; } inline int next_int(int mod) { return next() % mod; } inline int next_int(int l, int r) { return l + next_int(r - l + 1); } inline double next_double() { return next_int() * e; } }; /** shuffle **/ template<typename T> void shuffle_vector(std::vector<T>& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } /** split **/ std::vector<std::string> split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector<std::string> parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; } /** misc **/ template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template<typename T, typename ...Args> auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector<T>(arg, x); else return std::vector(arg, make_vector<T>(x, args...)); } template<typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template<typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /** using **/ using ll = long long; using ull = unsigned long long; using ld = double; //using ld = boost::multiprecision::cpp_bin_float_quad; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<typename T> using PQ = std::priority_queue<T>; template<typename T> using MPQ = std::priority_queue<T, std::vector<T>, std::greater<T>>; using namespace std; template<typename T> void print_vector(const std::vector<T>& v) { for (int i = 0; i < v.size(); i++) { std::cout << (i ? " " : "") << v[i]; } std::cout << '\n'; } #if 1 inline double get_temp(double stemp, double etemp, double t, double T) { return etemp + (stemp - etemp) * (T - t) / T; }; #else inline double get_temp(double stemp, double etemp, double t, double T) { return stemp * pow(etemp / stemp, t / T); }; #endif void solve() { int N, M; cin >> N >> M; vector<vector<int>> G(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); } constexpr int inf = INT_MAX / 8; auto get_dist = [&](int s) { vector<int> dist(N, inf); queue<int> qu; dist[s] = 0; qu.push(s); while (!qu.empty()) { int u = qu.front(); qu.pop(); for (int v : G[u]) { if (dist[v] != inf) continue; dist[v] = dist[u] + 1; qu.push(v); } } return dist; }; auto d0 = get_dist(0); auto d1 = get_dist(N - 2); auto d2 = get_dist(N - 1); int ans = inf; // 0 -> N-2 -> N-1 -> 0 chmin(ans, d0[N - 2] + d1[N - 1] + d2[0]); chmin(ans, d0[N - 1] + d2[N - 2] + d1[0]); cout << (ans == inf ? -1 : ans) << endl; } int main() { int T = 1; //cin >> T; for (int t = 0; t < T; t++) { solve(); } return 0; }