#line 2 "/home/shogo314/cpp_include/ou-library/graph.hpp" #include #include #include #include /** * @brief グラフの汎用クラス * * @tparam Cost 辺のコストの型 */ template struct Graph { /** * @brief 有向辺の構造体 * * operator int()を定義しているので、int型にキャストすると勝手にdstになる * 例えば、 * for (auto& e : g[v]) をすると、vから出る辺が列挙されるが、 * for (int dst : g[v]) とすると、vから出る辺の行き先が列挙される */ struct Edge { int src; //!< 始点 int dst; //!< 終点 Cost cost; //!< コスト int id; //!< 辺の番号(追加された順、無向辺の場合はidが同じで方向が逆のものが2つ存在する) Edge() = default; Edge(int src, int dst, Cost cost=1, int id=-1) : src(src), dst(dst), cost(cost), id(id) {} operator int() const { return dst; } }; int n; //!< 頂点数 int m; //!< 辺数 std::vector> g; //!< グラフの隣接リスト表現 /** * @brief デフォルトコンストラクタ */ Graph() : n(0), m(0), g(0) {} /** * @brief コンストラクタ * @param n 頂点数 */ explicit Graph(int n) : n(n), m(0), g(n) {} /** * @brief 無向辺を追加する * @param u 始点 * @param v 終点 * @param w コスト 省略したら1 */ void add_edge(int u, int v, Cost w=1) { g[u].push_back({u, v, w, m}); g[v].push_back({v, u, w, m++}); } /** * @brief 有向辺を追加する * @param u 始点 * @param v 終点 * @param w コスト 省略したら1 */ void add_directed_edge(int u, int v, Cost w=1) { g[u].push_back({u, v, w, m++}); } /** * @brief 辺の情報を標準入力から受け取って追加する * @param m 辺の数 * @param padding 頂点番号を入力からいくつずらすか 省略したら-1 * @param weighted 辺の重みが入力されるか 省略したらfalseとなり、重み1で辺が追加される * @param directed 有向グラフかどうか 省略したらfalse */ void read(int m, int padding=-1, bool weighted=false, bool directed=false) { for(int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; u += padding, v += padding; Cost c(1); if(weighted) std::cin >> c; if(directed) add_directed_edge(u, v, c); else add_edge(u, v, c); } } /** * @brief ある頂点から出る辺を列挙する * @param v 頂点番号 * @return std::vector& vから出る辺のリスト */ std::vector& operator[](int v) { return g[v]; } /** * @brief ある頂点から出る辺を列挙する * @param v 頂点番号 * @return const std::vector& vから出る辺のリスト */ const std::vector& operator[](int v) const { return g[v]; } /** * @brief 辺のリスト * @return std::vector 辺のリスト(idの昇順) * * 無向辺は代表して1つだけ格納される */ std::vector edges() const { std::vector res(m); for(int i = 0; i < n; i++) { for(auto& e : g[i]) { res[e.id] = e; } } return res; } /** * @brief ある頂点から各頂点への最短路 * * @param s 始点 * @param weighted 1以外のコストの辺が存在するか 省略するとtrue * @param inf コストのminの単位元 未到達の頂点への距離はinfになる 省略すると-1 * @return std::pair, std::vector> first:各頂点への最短路長 second:各頂点への最短路上の直前の辺 */ std::pair, std::vector> shortest_path(int s, bool weignted = true, Cost inf = -1) const { if(weignted) return shortest_path_dijkstra(s, inf); return shortest_path_bfs(s, inf); } std::vector topological_sort() { std::vector indeg(n), sorted; std::queue q; for (int i = 0; i < n; i++) { for (int dst : g[i]) indeg[dst]++; } for (int i = 0; i < n; i++) { if (!indeg[i]) q.push(i); } while (!q.empty()) { int cur = q.front(); q.pop(); for (int dst : g[cur]) { if (!--indeg[dst]) q.push(dst); } sorted.push_back(cur); } return sorted; } private: std::pair, std::vector> shortest_path_bfs(int s, Cost inf) const { std::vector dist(n, inf); std::vector prev(n); std::queue que; dist[s] = 0; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); for(auto& e : g[u]) { if(dist[e.dst] == inf) { dist[e.dst] = dist[e.src] + 1; prev[e.dst] = e; que.push(e.dst); } } } return {dist, prev}; } std::pair, std::vector> shortest_path_dijkstra(int s, Cost inf) const { std::vector dist(n, inf); std::vector prev(n); using Node = std::pair; std::priority_queue, std::greater> que; dist[s] = 0; que.push({0, s}); while(!que.empty()) { auto [d, u] = que.top(); que.pop(); if(d > dist[u]) continue; for(auto& e : g[u]) { if(dist[e.dst] == inf || dist[e.dst] > dist[e.src] + e.cost) { dist[e.dst] = dist[e.src] + e.cost; prev[e.dst] = e; que.push({dist[e.dst], e.dst}); } } } return {dist, prev}; } }; #line 2 "/home/shogo314/cpp_include/ou-library/io.hpp" /** * @file io.hpp * @brief 空白区切り出力、iostreamのオーバーロード */ #include #line 9 "/home/shogo314/cpp_include/ou-library/io.hpp" #include #include #line 12 "/home/shogo314/cpp_include/ou-library/io.hpp" namespace tuple_io { template std::basic_istream& read_tuple(std::basic_istream& is, Tuple& t) { is >> std::get(t); if constexpr (I + 1 < std::tuple_size_v) { return read_tuple(is, t); } return is; } template std::basic_ostream& write_tuple(std::basic_ostream& os, const Tuple& t) { os << std::get(t); if constexpr (I + 1 < std::tuple_size_v) { os << CharT(' '); return write_tuple(os, t); } return os; } }; template std::basic_istream& operator>>(std::basic_istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template std::basic_istream& operator>>(std::basic_istream& is, std::tuple& p) { return tuple_io::read_tuple, 0>(is, p); } template std::basic_istream& operator>>(std::basic_istream& is, std::array& a) { for(auto& e : a) is >> e; return is; } template std::basic_istream& operator>>(std::basic_istream& is, std::vector& v) { for(auto& e : v) is >> e; return is; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::pair& p) { os << p.first << CharT(' ') << p.second; return os; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::tuple& p) { return tuple_io::write_tuple, 0>(os, p); } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::array& a) { for(size_t i = 0; i < N; ++i) { if(i) os << CharT(' '); os << a[i]; } return os; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::vector& v) { for(size_t i = 0; i < v.size(); ++i) { if(i) os << CharT(' '); os << v[i]; } return os; } /** * @brief 空行出力 */ void print() { std::cout << '\n'; } /** * @brief 出力して改行 * * @tparam T 型 * @param x 出力する値 */ template void print(const T& x) { std::cout << x << '\n'; } /** * @brief 空白区切りで出力して改行 * * @tparam T 1つ目の要素の型 * @tparam Tail 2つ目以降の要素の型 * @param x 1つ目の要素 * @param tail 2つ目以降の要素 */ template void print(const T& x, const Tail&... tail) { std::cout << x << ' '; print(tail...); } #line 1 "/home/shogo314/cpp_include/sh-library/base.hpp" #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using str = string; using pl = pair; template using ml = map; using mll = map; using sl = set; using ld = long double; using pd = pair; template using vec = vector; template using vv = vector>; template using vvv = vector>>; template using vp = vector>; using vl = vec; using vvl = vv; using vs = vec; using vc = vec; using vpl = vec; using spl = set; using vd = vec; using vpd = vec; template using ary = array; template using al = array; template using aal = array, N1>; template using val = vec>; #define all(obj) (obj).begin(), (obj).end() #define reps(i, a, n) for (ll i = (a); i < ll(n); i++) #define rep(i, n) reps(i, 0, (n)) #define rrep(i, n) reps(i, 1, (n) + 1) #define repds(i, a, n) for (ll i = ((n) - 1); i >= (a); i--) #define repd(i, n) repds(i, 0, (n)) #define rrepd(i, n) repds(i, 1, (n) + 1) #define rep2(i, j, x, y) rep(i, x) rep(j, y) template inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } #define LL(x) ll x;cin >> x; #define VL(a,n) vl a(n);cin >> a; #define VS(a,n) vs a(n);cin >> a; #define STR(s) str s;cin >> s; #define VPL(a,n) vpl a(n);cin >> a; #define VAL(a,n,k) val a(n);cin >> a; #line 4 "main.cpp" void solve() { LL(N); LL(M); Graph g(4 * N); rep(i, M) { LL(u); LL(v); u--; v--; g.add_directed_edge(4 * u, 4 * v); g.add_directed_edge(4 * u + 1, 4 * v + 1); g.add_directed_edge(4 * u + 2, 4 * v + 2); g.add_directed_edge(4 * u + 3, 4 * v + 3); } g.add_directed_edge(4 * (N - 2), 4 * (N - 2) + 1, 0); g.add_directed_edge(4 * (N - 2) + 2, 4 * (N - 2) + 3, 0); g.add_directed_edge(4 * (N - 1), 4 * (N - 1) + 2, 0); g.add_directed_edge(4 * (N - 1) + 1, 4 * (N - 1) + 3, 0); ll ans = g.shortest_path(0, 3).first[3]; print(ans); } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); solve(); }