結果

問題 No.2565 はじめてのおつかい
ユーザー shogo314shogo314
提出日時 2023-12-02 14:37:24
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 12,375 bytes
コンパイル時間 3,629 ms
コンパイル使用メモリ 268,736 KB
実行使用メモリ 50,124 KB
最終ジャッジ日時 2023-12-02 14:37:35
合計ジャッジ時間 8,185 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 94 ms
50,124 KB
testcase_04 AC 61 ms
50,124 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 59 ms
21,248 KB
testcase_07 AC 27 ms
12,928 KB
testcase_08 AC 32 ms
14,720 KB
testcase_09 AC 50 ms
19,072 KB
testcase_10 AC 36 ms
16,384 KB
testcase_11 AC 92 ms
36,920 KB
testcase_12 AC 16 ms
9,344 KB
testcase_13 AC 40 ms
15,360 KB
testcase_14 AC 64 ms
26,248 KB
testcase_15 AC 73 ms
24,576 KB
testcase_16 AC 58 ms
44,884 KB
testcase_17 AC 13 ms
9,984 KB
testcase_18 AC 23 ms
19,348 KB
testcase_19 AC 72 ms
32,780 KB
testcase_20 AC 69 ms
30,152 KB
testcase_21 AC 63 ms
30,376 KB
testcase_22 AC 31 ms
20,844 KB
testcase_23 AC 44 ms
21,248 KB
testcase_24 AC 7 ms
7,040 KB
testcase_25 AC 64 ms
30,688 KB
testcase_26 AC 37 ms
19,796 KB
testcase_27 AC 52 ms
32,420 KB
testcase_28 AC 67 ms
32,000 KB
testcase_29 AC 81 ms
39,808 KB
testcase_30 AC 57 ms
35,432 KB
testcase_31 AC 68 ms
31,436 KB
testcase_32 AC 30 ms
19,636 KB
testcase_33 AC 54 ms
33,712 KB
testcase_34 AC 62 ms
28,420 KB
testcase_35 AC 60 ms
39,736 KB
testcase_36 AC 89 ms
33,608 KB
testcase_37 AC 38 ms
20,480 KB
testcase_38 AC 51 ms
28,620 KB
testcase_39 AC 64 ms
23,936 KB
testcase_40 AC 73 ms
39,372 KB
testcase_41 AC 80 ms
41,988 KB
testcase_42 AC 38 ms
19,144 KB
testcase_43 AC 45 ms
26,432 KB
testcase_44 AC 30 ms
14,976 KB
testcase_45 AC 13 ms
10,752 KB
testcase_46 AC 71 ms
26,112 KB
testcase_47 AC 40 ms
16,768 KB
testcase_48 AC 41 ms
15,744 KB
testcase_49 AC 17 ms
9,088 KB
testcase_50 AC 49 ms
18,304 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 26 ms
18,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/home/shogo314/cpp_include/ou-library/graph.hpp"

#include <iostream>
#include <limits>
#include <queue>
#include <vector>

/**
 * @brief グラフの汎用クラス
 *
 * @tparam Cost 辺のコストの型
 */
template <typename Cost=int>
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<std::vector<Edge>> 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<Edge>& vから出る辺のリスト
     */
    std::vector<Edge>& operator[](int v) {
        return g[v];
    }
    /**
     * @brief ある頂点から出る辺を列挙する
     * @param v 頂点番号
     * @return const std::vector<Edge>& vから出る辺のリスト
     */
    const std::vector<Edge>& operator[](int v) const {
        return g[v];
    }
    /**
     * @brief 辺のリスト
     * @return std::vector<Edge> 辺のリスト(idの昇順)
     *
     * 無向辺は代表して1つだけ格納される
     */
    std::vector<Edge> edges() const {
        std::vector<Edge> 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<Cost>, std::vector<Edge>> first:各頂点への最短路長 second:各頂点への最短路上の直前の辺
     */
    std::pair<std::vector<Cost>, std::vector<Edge>> 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<int> topological_sort() {
        std::vector<int> indeg(n), sorted;
        std::queue<int> 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<Cost>, std::vector<Edge>> shortest_path_bfs(int s, Cost inf) const {
        std::vector<Cost> dist(n, inf);
        std::vector<Edge> prev(n);
        std::queue<int> 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<Cost>, std::vector<Edge>> shortest_path_dijkstra(int s, Cost inf) const {
        std::vector<Cost> dist(n, inf);
        std::vector<Edge> prev(n);
        using Node = std::pair<Cost, int>;
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> 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 <array>
#line 9 "/home/shogo314/cpp_include/ou-library/io.hpp"
#include <utility>
#include <tuple>
#line 12 "/home/shogo314/cpp_include/ou-library/io.hpp"

namespace tuple_io {
    template <typename Tuple, size_t I, typename CharT, typename Traits>
    std::basic_istream<CharT, Traits>& read_tuple(std::basic_istream<CharT, Traits>& is, Tuple& t) {
        is >> std::get<I>(t);
        if constexpr (I + 1 < std::tuple_size_v<Tuple>) {
            return read_tuple<Tuple, I + 1>(is, t);
        }
        return is;
    }
    template <typename Tuple, size_t I, typename CharT, typename Traits>
    std::basic_ostream<CharT, Traits>& write_tuple(std::basic_ostream<CharT, Traits>& os, const Tuple& t) {
        os << std::get<I>(t);
        if constexpr (I + 1 < std::tuple_size_v<Tuple>) {
            os << CharT(' ');
            return write_tuple<Tuple, I + 1>(os, t);
        }
        return os;
    }
};

template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::pair<T1, T2>& p) {
    is >> p.first >> p.second;
    return is;
}
template <typename... Types, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::tuple<Types...>& p) {
    return tuple_io::read_tuple<std::tuple<Types...>, 0>(is, p);
}
template <typename T, size_t N, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::array<T, N>& a) {
    for(auto& e : a) is >> e;
    return is;
}
template <typename T, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::vector<T>& v) {
    for(auto& e : v) is >> e;
    return is;
}

template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::pair<T1, T2>& p) {
    os << p.first << CharT(' ') << p.second;
    return os;
}
template <typename... Types, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::tuple<Types...>& p) {
    return tuple_io::write_tuple<std::tuple<Types...>, 0>(os, p);
}
template <typename T, size_t N, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::array<T, N>& a) {
    for(size_t i = 0; i < N; ++i) {
        if(i) os << CharT(' ');
        os << a[i];
    }
    return os;
}
template <typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::vector<T>& 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 <typename T>
void print(const T& x) { std::cout << x << '\n'; }
/**
 * @brief 空白区切りで出力して改行
 * 
 * @tparam T 1つ目の要素の型
 * @tparam Tail 2つ目以降の要素の型
 * @param x 1つ目の要素
 * @param tail 2つ目以降の要素
 */
template <typename T, typename... Tail>
void print(const T& x, const Tail&... tail) {
    std::cout << x << ' ';
    print(tail...);
}
#line 1 "/home/shogo314/cpp_include/sh-library/base.hpp"
#include <bits/stdc++.h>

#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<ll, ll>;
template <typename T>
using ml = map<ll, T>;
using mll = map<ll, ll>;
using sl = set<ll>;
using ld = long double;
using pd = pair<ld, ld>;
template <typename T>
using vec = vector<T>;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vector<vector<T>>>;
template <typename T1, typename T2>
using vp = vector<pair<T1, T2>>;
using vl = vec<ll>;
using vvl = vv<ll>;
using vs = vec<str>;
using vc = vec<char>;
using vpl = vec<pl>;
using spl = set<pl>;
using vd = vec<ld>;
using vpd = vec<pd>;
template <typename T, int N>
using ary = array<T, N>;
template <int N>
using al = array<ll, N>;
template <int N1, int N2>
using aal = array<array<ll, N2>, N1>;
template <int N>
using val = vec<al<N>>;

#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 <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T1, typename T2>
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<k> a(n);cin >> a;
#line 4 "main.cpp"

void solve() {
    LL(N);
    LL(M);
    Graph<ll> 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();
}
0