結果
| 問題 |
No.2565 はじめてのおつかい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 14:37:24 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 2,000 ms |
| コード長 | 12,375 bytes |
| コンパイル時間 | 3,692 ms |
| コンパイル使用メモリ | 267,920 KB |
| 実行使用メモリ | 50,048 KB |
| 最終ジャッジ日時 | 2024-09-26 16:59:34 |
| 合計ジャッジ時間 | 7,749 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
#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();
}