結果

問題 No.705 ゴミ拾い Hard
ユーザー hitonanodehitonanode
提出日時 2024-10-06 15:01:53
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 1,500 ms
コード長 3,559 bytes
コンパイル時間 1,511 ms
コンパイル使用メモリ 102,784 KB
実行使用メモリ 10,304 KB
最終ジャッジ日時 2024-10-06 15:01:59
合計ジャッジ時間 5,048 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 108 ms
10,240 KB
testcase_25 AC 106 ms
10,240 KB
testcase_26 AC 106 ms
10,112 KB
testcase_27 AC 107 ms
10,240 KB
testcase_28 AC 107 ms
10,112 KB
testcase_29 AC 108 ms
10,112 KB
testcase_30 AC 114 ms
10,112 KB
testcase_31 AC 102 ms
10,112 KB
testcase_32 AC 103 ms
10,112 KB
testcase_33 AC 70 ms
10,112 KB
testcase_34 AC 69 ms
10,208 KB
testcase_35 AC 80 ms
10,112 KB
testcase_36 AC 88 ms
10,240 KB
testcase_37 AC 79 ms
10,304 KB
testcase_38 AC 89 ms
10,240 KB
testcase_39 AC 107 ms
10,240 KB
testcase_40 AC 2 ms
5,248 KB
testcase_41 AC 2 ms
5,248 KB
testcase_42 AC 2 ms
5,248 KB
testcase_43 AC 102 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "other_algorithms/test/monge_shortest_path.yuki705.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/705"
#line 2 "other_algorithms/monge_shortest_path.hpp"
#include <cassert>
#include <vector>

// Shortest path of Monge-weighted graph
// Variant of LARSCH Algorithm: https://noshi91.hatenablog.com/entry/2023/02/18/005856
// Complexity: O(n log n)
//
// Given a directed graph with n vertices and weighted edges
// (w(i, j) = cost_callback(i, j) (i < j)),
// this class calculates the shortest path from vertex 0 to all other vertices.
template <class Cost> struct monge_shortest_path {
    std::vector<Cost> dist; // dist[i] = shortest distance from 0 to i
    std::vector<int> amin;  // amin[i] = previous vertex of i in the shortest path

    template <class F> void _check(int i, int k, F cost_callback) {
        if (i <= k) return;
        if (Cost c = dist[k] + cost_callback(k, i); c < dist[i]) dist[i] = c, amin[i] = k;
    }

    template <class F> void _rec_solve(int l, int r, F cost_callback) {
        if (r - l == 1) return;

        const int m = (l + r) / 2;
        for (int k = amin[l]; k <= amin[r]; ++k) _check(m, k, cost_callback);

        _rec_solve(l, m, cost_callback);
        for (int k = l + 1; k <= m; ++k) _check(r, k, cost_callback);
        _rec_solve(m, r, cost_callback);
    }

    template <class F> Cost solve(int n, F cost_callback) {
        assert(n > 0);
        dist.resize(n);
        amin.assign(n, 0);

        dist[0] = Cost();
        for (int i = 1; i < n; ++i) dist[i] = cost_callback(0, i);

        _rec_solve(0, n - 1, cost_callback);

        return dist.back();
    }

    template <class F> int num_edges() const {
        int ret = 0;
        for (int c = (int)amin.size() - 1; c >= 0; c = amin[c]) ++ret;
        return ret;
    }
};

// Find shortest path length from 0 to n - 1 with k edges, min_edges <= k <= max_edges
// https://noshi91.hatenablog.com/entry/2022/01/13/001217
template <class Cost, class F>
Cost monge_shortest_path_with_specified_edges(int n, int min_edges, int max_edges,
                                              Cost max_abs_cost, F cost_callback) {

    assert(1 <= n);
    assert(0 <= min_edges);
    assert(min_edges <= max_edges);
    assert(max_edges <= n - 1);

    monge_shortest_path<Cost> msp;

    auto eval = [&](Cost p) -> Cost {
        msp.solve(n, [&](int i, int j) { return cost_callback(i, j) - p; });
        return -msp.dist.back() - p * (p < 0 ? max_edges : min_edges);
    };

    Cost lo = -max_abs_cost * 3, hi = max_abs_cost * 3;

    while (lo + 1 < hi) {
        Cost p = (lo + hi) / 2, f = eval(p), df = eval(p + 1) - f;
        if (df == Cost()) {
            return -f;
        } else {
            (df < Cost() ? lo : hi) = p;
        }
    }

    Cost flo = eval(lo), fhi = eval(hi);

    return flo < fhi ? -flo : -fhi;
}
#line 3 "other_algorithms/test/monge_shortest_path.yuki705.test.cpp"

#line 5 "other_algorithms/test/monge_shortest_path.yuki705.test.cpp"
#include <cmath>
#include <iostream>
using namespace std;

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

    int N;
    cin >> N;
    vector<int> A(N), X(N), Y(N);
    for (auto &a : A) cin >> a;
    for (auto &x : X) cin >> x;
    for (auto &y : Y) cin >> y;

    auto weight = [&](int j, int i) {
        assert(j < i);
        --i;
        const long long dx = abs(A.at(i) - X.at(j)), dy = Y.at(j);
        return dx * dx * dx + dy * dy * dy;
    };

    monge_shortest_path<long long> msp;
    cout << msp.solve(N + 1, weight) << '\n';
}
0