結果

問題 No.957 植林
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-25 03:38:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 3,431 bytes
コンパイル時間 3,375 ms
コンパイル使用メモリ 267,736 KB
実行使用メモリ 9,784 KB
最終ジャッジ日時 2024-04-27 15:29:46
合計ジャッジ時間 7,849 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 34 ms
8,968 KB
testcase_04 AC 61 ms
8,536 KB
testcase_05 AC 24 ms
9,108 KB
testcase_06 AC 174 ms
9,220 KB
testcase_07 AC 32 ms
8,820 KB
testcase_08 AC 18 ms
9,044 KB
testcase_09 AC 18 ms
9,036 KB
testcase_10 AC 18 ms
9,252 KB
testcase_11 AC 16 ms
8,960 KB
testcase_12 AC 17 ms
9,040 KB
testcase_13 AC 15 ms
8,020 KB
testcase_14 AC 18 ms
9,284 KB
testcase_15 AC 17 ms
8,940 KB
testcase_16 AC 14 ms
8,024 KB
testcase_17 AC 15 ms
8,680 KB
testcase_18 AC 84 ms
8,944 KB
testcase_19 AC 88 ms
9,004 KB
testcase_20 AC 91 ms
9,216 KB
testcase_21 AC 96 ms
9,208 KB
testcase_22 AC 95 ms
9,344 KB
testcase_23 AC 99 ms
9,488 KB
testcase_24 AC 104 ms
9,632 KB
testcase_25 AC 107 ms
9,656 KB
testcase_26 AC 106 ms
9,656 KB
testcase_27 AC 109 ms
9,660 KB
testcase_28 AC 107 ms
9,532 KB
testcase_29 AC 106 ms
9,656 KB
testcase_30 AC 106 ms
9,660 KB
testcase_31 AC 82 ms
8,944 KB
testcase_32 AC 86 ms
9,064 KB
testcase_33 AC 89 ms
9,216 KB
testcase_34 AC 92 ms
9,080 KB
testcase_35 AC 96 ms
9,216 KB
testcase_36 AC 99 ms
9,360 KB
testcase_37 AC 104 ms
9,636 KB
testcase_38 AC 108 ms
9,784 KB
testcase_39 AC 111 ms
9,652 KB
testcase_40 AC 109 ms
9,652 KB
testcase_41 AC 14 ms
9,660 KB
testcase_42 AC 15 ms
9,528 KB
testcase_43 AC 22 ms
9,652 KB
testcase_44 AC 23 ms
9,656 KB
testcase_45 AC 2 ms
6,944 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define REP(i, n) FOR(i, 0, n)

template <class Cap, Cap INF = std::numeric_limits<Cap>::max() / 2> struct mf_pushrelabel {
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::vector<_edge>> g;
    std::vector<std::pair<int, int>> pos;
    mf_pushrelabel(int n) : _n(n), g(n) { static_assert(INF > 0, "INF must be positive."); }
    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from and from < _n);
        assert(0 <= to and to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.emplace_back(from, int(g[from].size()));
        int from_id = g[from].size(), to_id = g[to].size() + (from == to);
        g[from].push_back({to, to_id, cap});
        g[to].push_back({from, from_id, Cap(0)});
        return m;
    }

    std::vector<int> dist;
    std::vector<Cap> excess;
    std::vector<std::vector<int>> h2v;
    int max_height;
    void activate(int i) {
        h2v[dist[i]].push_back(i);
        if (dist[i] > max_height) max_height = dist[i];
    }
    Cap flow(int s, int t) {
        assert(0 <= s and s < _n);
        assert(0 <= t and t < _n);
        assert(s != t);
        dist.assign(_n, 0);
        dist[s] = _n;
        excess.assign(_n, 0);
        excess[s] = INF, excess[t] = -INF;
        h2v.assign(_n * 2, {});
        max_height = -1;
        for (auto &e : g[s]) push(s, e);
        while (max_height >= 0) {
            if (h2v[max_height].empty()) {
                max_height--;
                continue;
            }
            int i = h2v[max_height].back();
            h2v[max_height].pop_back();
            int dnxt = _n * 2 - 1;
            for (auto &e : g[i]) {
                if (!e.cap) continue;
                if (dist[e.to] == dist[i] - 1) {
                    push(i, e);
                    if (excess[i] == 0) break;
                } else {
                    if (dist[e.to] + 1 < dnxt) dnxt = dist[e.to] + 1;
                }
            }
            if (excess[i] > 0) {
                dist[i] = dnxt;
                activate(i);
            }
        }
        return INF - excess[s];
    }

    void push(int i, _edge &e) {
        Cap delta = e.cap < excess[i] ? e.cap : excess[i];
        excess[i] -= delta, e.cap -= delta;
        excess[e.to] += delta, g[e.to][e.rev].cap += delta;
        if (excess[e.to] > 0 and excess[e.to] <= delta) activate(e.to);
    }
};

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

    int H, W;
    cin >> H >> W;
    vector<vector<lint>> G(H, vector<lint>(W));
    for (auto &v : G) {
        for (auto &x : v) cin >> x;
    }
    vector<lint> R(H), C(W);
    for (auto &x : R) cin >> x;
    for (auto &x : C) cin >> x;
    lint tot = accumulate(ALL(R), 0LL) + accumulate(ALL(C), 0LL);

    vector<lint> EW(W);
    lint f1 = 0;

    int Z = 1 + H + W;
    mf_pushrelabel<long long> g(Z + 1);

    REP(i, H) {
        lint gtot = accumulate(ALL(G[i]), 0LL);
        lint f0 = min(gtot, R[i]);
        f1 += f0;
        g.add_edge(0, i + 1, gtot - f0);
    }
    REP(j, W) g.add_edge(H + 1 + j, Z, C[j]);
    REP(i, H) REP(j, W) g.add_edge(i + 1, H + 1 + j, G[i][j]);
    lint f2 = g.flow(0, Z);
    cout << tot - f1 - f2 << endl;
}
0