結果

問題 No.2695 Warp Zone
ユーザー みしあみしあ
提出日時 2024-03-27 01:48:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 10,548 bytes
コンパイル時間 5,527 ms
コンパイル使用メモリ 299,552 KB
実行使用メモリ 101,120 KB
最終ジャッジ日時 2024-09-30 14:29:12
合計ジャッジ時間 7,849 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 164 ms
101,120 KB
testcase_04 AC 160 ms
99,072 KB
testcase_05 AC 162 ms
99,072 KB
testcase_06 AC 164 ms
100,608 KB
testcase_07 AC 166 ms
100,864 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 61 ms
45,824 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 18 ms
16,256 KB
testcase_12 AC 83 ms
59,648 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 106 ms
71,808 KB
testcase_15 AC 54 ms
41,088 KB
testcase_16 AC 15 ms
13,696 KB
testcase_17 AC 27 ms
22,784 KB
testcase_18 AC 127 ms
84,992 KB
testcase_19 AC 3 ms
5,248 KB
testcase_20 AC 112 ms
77,312 KB
testcase_21 AC 98 ms
70,912 KB
testcase_22 AC 35 ms
27,904 KB
testcase_23 AC 84 ms
59,136 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 1 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region Macros
#if defined(ONLINE_JUDGE)
#include <atcoder/all>
#endif
#if defined(LOCAL) || defined(_DEBUG)
#include "template.hpp"
#else
#include <bits/stdc++.h>
#include <boost/format.hpp>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define O(...)
#define OP(...)
#define START()
#define STOP()
#define MEMORY()
#define massert(x, msec) if(!(x)) {\
    while(msec > chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - _MY_START_TIME).count()); \
    cerr << "assert [" << __FUNCTION__ << ":" << to_string(__LINE__) << "] : " << string(#x) << el; exit(1);\
}
/******************************************* マクロ等 **************************************************/
#define REP(i, n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(ll i=(n); i>=0; --i)
#define FOR(i, n, m) for(ll i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(auto&& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define FILL(v, h) memset((v), h, sizeof(v))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
vector<long long> __FREP_CNT_ARRAY;
#define FREP(i, n) ;__FREP_CNT_ARRAY.push_back(n); REP(i, n) {
#define FREPR(i, n) ;__FREP_CNT_ARRAY.push_back((n)+1); REPR(i, n) {
#define FFOR(i, n, m) ;__FREP_CNT_ARRAY.push_back((n)-(m)); FOR(i, n, m) {
#define FEACH(a, as) ;__FREP_CNT_ARRAY.push_back((as).size()); EACH(a, as) {
#define THEN __FREP_CNT_ARRAY.back()--;} if(bool frep_tmp = !__FREP_CNT_ARRAY.back(); __FREP_CNT_ARRAY.pop_back(), frep_tmp)
#define DPINIT(name) name; bool __SEEN_##name;
#define DP(name) name;if(__SEEN_##name)return name;__SEEN_##name=true;
template<class T, class U>bool chmax(T &a, const U &b) { if (a<(T)b) { a=(T)b; return 1; } return 0; }
template<class T, class U>bool chmin(T &a, const U &b) { if (b<(T)a) { a=(T)b; return 1; } return 0; }
#define cmp3(a, x, b) ((a) <= (x) && (x) < (b))
#define vec vector
#define umap unordered_map
#define uset unordered_set
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll, ll>;
using Tup = tuple<ll, ll, ll>;
using vl = vec<ll>;
using vvl = vec<vl>;
#define fi first
#define se second
#define pb push_back
#define el '\n'
constexpr ll INF = numeric_limits<ll>::max()/4-1;
/******************************************* 入力 **************************************************/
template<class T> istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T, class U> istream &operator>>(istream &stream, pair<T, U>& o){cin >> o.fi >> o.se; return stream;}
namespace myinput {
    void input() {}
    template<class T, class... Ts> void input(T&& o, Ts&&... args){cin >> o;input(forward<Ts>(args)...);}
    void assign_vl(size_t) {};
    template<class T, class... Ts> void assign_vl(size_t siz, T&& o, Ts&&... args){o.resize(siz);assign_vl(siz, forward<Ts>(args)...);}
    void input_horizon_sub(size_t) {};
    template<class T, class... Ts> void input_horizon_sub(size_t index, T&& o, Ts&&... args) {cin>>o[index];input_horizon_sub(index, forward<Ts>(args)...);}
    template<class... Ts> void input_horizon(size_t siz, Ts&&... args){REP(i, siz) input_horizon_sub(i, forward<Ts>(args)...);}
}
#define _I(T, ...) ;myinput::input(__VA_ARGS__);
#define I(T, ...) ;T __VA_ARGS__;_I(T, __VA_ARGS__);
#define _Iv(T, siz, ...) ;myinput::assign_vl(siz, __VA_ARGS__);myinput::input(__VA_ARGS__);
#define Iv(T, siz, ...) ;vec<T> __VA_ARGS__;_Iv(T, siz, __VA_ARGS__);
#define _Ih(T, siz, ...) ;myinput::assign_vl(siz, __VA_ARGS__);myinput::input_horizon(siz, __VA_ARGS__);
#define Ih(T, siz, ...) ;vec<T> __VA_ARGS__;_Ih(T, siz, __VA_ARGS__);
chrono::system_clock::time_point _MY_START_TIME;
/******************************************* 出力 **************************************************/
namespace __PrintHelper {
    template <char Separator = ' ', typename T>
    void printValue(std::ostream& os, const T& value) { os << value; }
    template <char Separator = ' ', typename T1, typename T2>
    void printValue(std::ostream& os, const std::pair<T1, T2>& value) { os << value.first << Separator << value.second; }
    template <char Separator = ' ', typename... Args, std::size_t... Is>
    void printTuple(std::ostream& os, const std::tuple<Args...>& value, std::index_sequence<Is...>) { ((os << (Is == 0 ? "" : string(1, Separator)) << std::get<Is>(value)), ...); }
    template <char Separator = ' ', typename... Args>
    void printValue(std::ostream& os, const std::tuple<Args...>& value) { printTuple<Separator>(os, value, std::index_sequence_for<Args...>{}); }
    template <char Separator = ' ', typename T>
    void printValue(std::ostream& os, const std::vector<T>& value) { for (std::size_t i = 0; i < value.size(); ++i) os << (i == 0 ? "" : string(1, Separator)) << value[i]; }
};
template <char Separator = ' ', typename Arg, typename... Args>
void OO(const Arg &x, const Args&... args) {
    __PrintHelper::printValue<Separator>(cout, x);
    if constexpr (sizeof...(args) > 0) {
        cout << Separator;
        OO<Separator>(args...);
    }
    else { cout << el; }
}
template <class T = string, class U = string>
void YN(bool x, T y = "Yes", U n = "No") {
    if(x)   cout << y << el;
    else    cout << n << el;
}
/******************************************* 配列操作 **************************************************/
namespace __ArrayHelper {
    template <typename... Args>
    auto flatten(const std::tuple<Args...>& t);
    template <typename T, std::size_t... Indices>
    auto flattenHelper(const T& t, std::index_sequence<Indices...>) { return std::make_tuple(std::get<Indices>(t)...); }
    template <typename T, typename U>
    auto flatten(const std::pair<T, U>& p) { return flatten(std::make_tuple(p.first, p.second)); }
    template <typename T>
    auto flatten(const T& value) { return std::make_tuple(value); }
    template <typename... Args, std::size_t... Indices>
    auto flattenTupleHelper(const std::tuple<Args...>& t, std::index_sequence<Indices...>) { return std::tuple_cat(flatten(std::get<Indices>(t))...); }
    template <typename... Args>
    auto flatten(const std::tuple<Args...>& t) { return flattenTupleHelper(t, std::index_sequence_for<Args...>{}); }
    template <typename Container>
    auto __flatten(const Container& container) {
        using ValueType = typename Container::value_type;
        using ResultType = decltype(__ArrayHelper::flatten(std::declval<ValueType>()));
        std::vector<ResultType> result;
        for (const auto& item : container) { result.emplace_back(__ArrayHelper::flatten(item)); }
        return result;
    }
    template <class T>
    auto __enumrate(const vec<T> &v) {
        vec<pair<ll, T>> ret(v.size());
        REP(i, v.size()) ret[i] = {i, v[i]};
        return ret;
    }
}
constexpr auto flatten = [](auto&& x) { return __ArrayHelper::__flatten(x); };
constexpr auto enumrate = [](auto&& x) { return __ArrayHelper::__enumrate(x); };
template <typename... Args>
auto zip(const Args&... args) {
    size_t minSize = std::min({args.size()...});
    std::vector<std::tuple<typename Args::value_type...>> result;
    for (size_t i = 0; i < minSize; ++i) { result.emplace_back(std::make_tuple(args[i]...)); }
    return result;
}
template <typename Arg>
Arg compose(Arg arg) { return arg; }
template <typename Arg, typename Func>
auto compose(Arg arg, Func func) { return func(arg); }
template <typename Arg, typename Func, typename... Funcs>
auto compose(Arg arg, Func func, Funcs... funcs) { return compose(compose(arg, func), funcs...); }
/******************************************* Main **************************************************/
#endif
#pragma endregion
using Point = complex<ll>;
void Main();

int main(){
    _MY_START_TIME = std::chrono::system_clock::now();
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(15);
    std::cerr << std::fixed << std::setprecision(15);
    Main();
    MEMORY();
    return 0;
}

#pragma region graph
struct edge{
    ll from, to, cost;
    bool operator<(const edge& r) const{return cost<r.cost;}
    bool operator>(const edge& r) const{return cost>r.cost;}
};
struct graph{
    ll V;
    vector<vector<edge> > G;
    ll height, width;
    graph(ll n){
        init(n);
    }
    void init(ll n){
        V = n;
        G.resize(V);
    }
    // 無向グラフ
    void add_edge(ll s, ll t, ll cost = 1){
        add_diedge(s, t, cost);
        add_diedge(t, s, cost);
    }
    void add_edge(Point s, Point t, ll cost = 1) { add_edge(pos(s), pos(t), cost); }
    // 有向グラフ
    void add_diedge(ll s, ll t, ll cost = 1){
        if(s < 0 || t < 0 || s >= V || t >= V) return;
        G[s].push_back({s, t, cost});
    }
    void add_diedge(Point s, Point t, ll cost = 1) { add_diedge(pos(s), pos(t), cost); }
    ll pos(ll y, ll x) { return ((0 <= y && y < height && 0 <= x && x < width) ? y*width + x : -1); }
    ll pos(Point p) { return pos(p.real(), p.imag()); }
    auto pos2d(ll h, ll w){ height = h; width = w; }
    int size() { return V; }
};
#pragma endregion

// O(|E|log|V|)
pair<umap<ll, ll>, vector<ll>> dijkstra(const graph& g, ll s){
    vector<ll> d(g.V, INF), prev(g.V, -1);
    umap<ll, ll> ret;
    ret[s] = d[s] = 0;
    priority_queue<P,vector<P>, greater<P>> que;
    que.push({0, s});
    while(!que.empty()){
        auto [c, v] = que.top(); que.pop();
        if(d[v]<c) continue;
        for(auto e : g.G[v]){
            if(d[e.to]>d[v]+e.cost){
                ret[e.to] = d[e.to] = d[v]+e.cost;
                prev[e.to] = v;
                que.push({d[e.to],e.to});
            }
        }
    }
    return {ret, prev};
}
vector<ll> get_path(const vector<ll> &prev, ll t) {
    vector<ll> path;
    for (int cur = t; cur != -1; cur = prev[cur]) {
        path.push_back(cur);
    }
    reverse(path.begin(), path.end());
    return path;
}

void Main() {
    I(ll, h, w, n);
    Ih(ll, n, as, bs, cs, ds);
    graph g(2*n + 2);
    REP(i, n) {
        g.add_diedge(2*i, 2*i+1, 1);
    }
    as.pb(1); bs.pb(1); cs.pb(h); ds.pb(w);
    REP(i, 2*n + 2) {
        FOR(j, 2*n + 2, i+1) {
            auto x = (i%2 == 0 ? P{as[i/2], bs[i/2]} : P{cs[i/2], ds[i/2]});
            auto y = (j%2 == 0 ? P{as[j/2], bs[j/2]} : P{cs[j/2], ds[j/2]});
            ll dist = abs(x.fi - y.fi) + abs(x.se - y.se);
            g.add_edge(i, j, dist);
        }
    }
    auto [d, _] = dijkstra(g, 2*n);
    OO(d[2*n+1]);
}

0