結果

問題 No.2354 Poor Sight in Winter
ユーザー 👑 nu50218nu50218
提出日時 2023-06-16 22:50:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 5,609 bytes
コンパイル時間 3,915 ms
コンパイル使用メモリ 248,368 KB
実行使用メモリ 21,832 KB
最終ジャッジ日時 2023-09-06 21:27:28
合計ジャッジ時間 6,901 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,388 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 162 ms
21,832 KB
testcase_12 AC 164 ms
21,808 KB
testcase_13 AC 176 ms
21,792 KB
testcase_14 AC 171 ms
21,780 KB
testcase_15 AC 170 ms
21,828 KB
testcase_16 AC 169 ms
21,636 KB
testcase_17 AC 172 ms
21,660 KB
testcase_18 AC 106 ms
11,024 KB
testcase_19 AC 131 ms
18,516 KB
testcase_20 AC 121 ms
11,832 KB
testcase_21 AC 9 ms
4,380 KB
testcase_22 AC 36 ms
7,344 KB
testcase_23 AC 36 ms
7,372 KB
testcase_24 AC 109 ms
18,636 KB
testcase_25 AC 42 ms
5,732 KB
testcase_26 AC 33 ms
5,112 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 7 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#ifdef LOCAL
#include <local.hpp>
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2")
#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define postprocess(...) ((void)0)
#endif

#line 1 "library/graph/shortest_path.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>
template <typename weight>
struct shortest_path {
    const weight unreachable = std::numeric_limits<weight>::max();

    shortest_path() : _computed(false) {}
    shortest_path(const int& n, const int& m = 0) : _n(n), _computed(false) {
        if (m) _edges.reserve(m);
    }

    void set_number(const int& n, const int& m = 0) {
        _n = n;
        if (m) _edges.reserve(m);
    }

    void add_edge(const int& i, const int& j, const weight& w) {
        assert(0 <= i && i < _n);
        assert(0 <= j && j < _n);
        _edges.emplace_back(i, j, w);
    }

    void compute(const int& s) {
        _s = s;

        _computed = true;

        _adj.resize(_n);
        for (auto&& e : _edges) {
            auto [u, v, w] = e;
            _adj[u].emplace_back(v, w);
        }

        _dist.resize(_n);
        std::fill(_dist.begin(), _dist.end(), unreachable);

        _par.resize(_n);
        std::fill(_par.begin(), _par.end(), -1);

        // select best algorithm
        if (!std::is_integral<weight>::value) {
            _dijkstra(s);
            return;
        }
        for (auto&& [_, __, cost] : _edges) {
            if (cost >= 2) {
                _dijkstra(s);
                return;
            }
        }
        for (auto&& [_, __, cost] : _edges) {
            if (cost == 0) {
                _bfs01(s);
                return;
            }
        }
        _bfs(s);
    }

    int s() {
        assert(_computed);
        return _s;
    }

    std::vector<weight> dist() {
        assert(_computed);
        return _dist;
    }

    weight dist(const int& t) {
        assert(_computed);
        return _dist[t];
    }

    std::vector<int> path(int t) {
        assert(_computed);
        assert(0 <= t && t < _n);
        assert(_dist[t] != unreachable);

        std::vector<int> ret;

        while (t != _s) {
            ret.push_back(t);
            t = _par[t];
        }
        ret.push_back(_s);

        std::reverse(ret.begin(), ret.end());
        return ret;
    }

   private:
    // input values
    int _n;
    int _s;
    std::vector<std::tuple<int, int, weight>> _edges;

    // computed values
    bool _computed;
    std::vector<std::vector<std::pair<int, weight>>> _adj;
    std::vector<weight> _dist;
    std::vector<int> _par;

    void _bfs(const int& s) {
        std::queue<int> que;

        que.emplace(s);
        _dist[s] = 0;

        while (!que.empty()) {
            auto v = que.front();
            que.pop();

            for (auto&& [to, _] : _adj[v]) {
                if (_dist[to] == unreachable) {
                    _dist[to] = _dist[v] + 1;
                    _par[to] = v;
                    que.emplace(to);
                }
            }
        }
    }

    void _bfs01(const int& s) {
        std::deque<int> que;

        _dist[s] = 0;
        que.emplace_back(s);

        while (!que.empty()) {
            auto v = que.front();
            que.pop_front();

            for (auto&& [to, cost] : _adj[v]) {
                weight d = _dist[v] + cost;
                if (d < _dist[to]) {
                    _dist[to] = d;
                    _par[to] = v;
                    if (cost) {
                        que.emplace_back(to);
                    } else {
                        que.emplace_front(to);
                    }
                }
            }
        }
    }

    void _dijkstra(const int& s) {
        using que_class = std::pair<weight, int>;
        std::priority_queue<que_class, std::vector<que_class>, std::greater<que_class>> que;

        _dist[s] = 0;
        que.emplace(0, s);

        while (!que.empty()) {
            auto [d, v] = que.top();
            que.pop();

            if (_dist[v] != d) continue;

            for (auto&& [to, cost] : _adj[v]) {
                if (_dist[to] <= d + cost) continue;

                _dist[to] = d + cost;
                _par[to] = v;
                que.emplace(_dist[to], to);
            }
        }
    }
};
#line 12 "main.cpp"

using namespace std;
using ll = long long;
using ld = long double;

void solve([[maybe_unused]] int test) {
    ll N, K;
    cin >> N >> K;

    const int s = 0;
    const int g = 1;
    vector<ll> x(N + 2), y(N + 2);
    for (int i = 0; i < N + 2; i++) {
        cin >> x[i] >> y[i];
    }

    ll dist[N + 2][N + 2];
    for (int i = 0; i < N + 2; i++) {
        for (int j = 0; j < N + 2; j++) {
            dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
        }
    }

    ll imin = 0;
    ll imax = 3e5;

    while (imax - imin > 1) {
        ll imid = (imin + imax) / 2;

        shortest_path<ll> sp(N + 2);

        for (int i = 0; i < N + 2; i++) {
            for (int j = 0; j < N + 2; j++) {
                if (i == j) continue;
                sp.add_edge(i, j, (dist[i][j] - 1) / imid);
                sp.add_edge(j, i, (dist[i][j] - 1) / imid);
            }
        }

        sp.compute(s);

        (sp.dist(g) != sp.unreachable && sp.dist(g) <= K ? imax : imin) = imid;
    }

    cout << imax << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve(i);
    }

    postprocess();
}
0