結果
問題 | No.957 植林 |
ユーザー | hitonanode |
提出日時 | 2021-08-27 01:36:09 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 155 ms / 2,000 ms |
コード長 | 6,433 bytes |
コンパイル時間 | 1,186 ms |
コンパイル使用メモリ | 94,304 KB |
実行使用メモリ | 7,644 KB |
最終ジャッジ日時 | 2024-11-19 01:33:53 |
合計ジャッジ時間 | 6,353 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 | 32 ms
7,192 KB |
testcase_04 | AC | 37 ms
7,124 KB |
testcase_05 | AC | 30 ms
7,208 KB |
testcase_06 | AC | 48 ms
7,220 KB |
testcase_07 | AC | 32 ms
7,176 KB |
testcase_08 | AC | 11 ms
7,072 KB |
testcase_09 | AC | 11 ms
7,196 KB |
testcase_10 | AC | 12 ms
7,216 KB |
testcase_11 | AC | 11 ms
7,200 KB |
testcase_12 | AC | 11 ms
7,200 KB |
testcase_13 | AC | 9 ms
7,108 KB |
testcase_14 | AC | 11 ms
7,360 KB |
testcase_15 | AC | 11 ms
7,196 KB |
testcase_16 | AC | 10 ms
7,168 KB |
testcase_17 | AC | 10 ms
7,172 KB |
testcase_18 | AC | 115 ms
7,068 KB |
testcase_19 | AC | 117 ms
7,072 KB |
testcase_20 | AC | 122 ms
7,336 KB |
testcase_21 | AC | 128 ms
7,156 KB |
testcase_22 | AC | 137 ms
7,360 KB |
testcase_23 | AC | 140 ms
7,364 KB |
testcase_24 | AC | 145 ms
7,384 KB |
testcase_25 | AC | 153 ms
7,644 KB |
testcase_26 | AC | 153 ms
7,568 KB |
testcase_27 | AC | 153 ms
7,516 KB |
testcase_28 | AC | 153 ms
7,644 KB |
testcase_29 | AC | 155 ms
7,640 KB |
testcase_30 | AC | 153 ms
7,644 KB |
testcase_31 | AC | 117 ms
7,192 KB |
testcase_32 | AC | 118 ms
7,204 KB |
testcase_33 | AC | 123 ms
7,208 KB |
testcase_34 | AC | 128 ms
7,224 KB |
testcase_35 | AC | 136 ms
7,356 KB |
testcase_36 | AC | 140 ms
7,372 KB |
testcase_37 | AC | 145 ms
7,380 KB |
testcase_38 | AC | 155 ms
7,528 KB |
testcase_39 | AC | 154 ms
7,644 KB |
testcase_40 | AC | 153 ms
7,512 KB |
testcase_41 | AC | 8 ms
7,644 KB |
testcase_42 | AC | 8 ms
7,516 KB |
testcase_43 | AC | 12 ms
7,644 KB |
testcase_44 | AC | 13 ms
7,516 KB |
testcase_45 | AC | 2 ms
5,248 KB |
testcase_46 | AC | 2 ms
5,248 KB |
testcase_47 | AC | 2 ms
5,248 KB |
ソースコード
#line 1 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/957" #line 2 "utilities/reader.hpp" #include <cstdio> #include <string> // CUT begin template <typename T> T rd_integer() { T ret = 0; bool minus = false; char c = getchar_unlocked(); while (!isdigit(c)) minus |= (c == '-'), c = getchar_unlocked(); while (isdigit(c)) ret = (ret << 1) + (ret << 3) + (c ^ 48), c = getchar_unlocked(); return minus ? -ret : ret; } int rdi() { return rd_integer<int>(); } long long rdll() { return rd_integer<long long>(); } std::string rdstr() { std::string ret; char c = getchar_unlocked(); while (!isgraph(c)) c = getchar_unlocked(); while (isgraph(c)) ret += c, c = getchar_unlocked(); return ret; } #line 2 "combinatorial_opt/maxflow_pushrelabel.hpp" #include <cassert> #include <iostream> #include <limits> #include <utility> #include <vector> // Maxflow (push-relabel, highest-label) // Complexity: O(N^2 M^(1/2)) template <class Cap, bool UseGlobalRelabeling, bool UseGapRelabeling, Cap INF> struct mf_pushrelabel { struct prique { std::vector<std::pair<int, int>> even_, odd_; int se, so; prique() = default; void init(int n) { even_.resize(n), odd_.resize(n), se = so = 0; }; void clear() { se = so = 0; } bool empty() const { return se + so == 0; } void push(int i, int h) { (h & 1 ? odd_[so++] : even_[se++]) = {i, h}; } int highest() const { int a = se ? even_[se - 1].second : -1, b = so ? odd_[so - 1].second : -1; return a > b ? a : b; } int pop() { if (!se or (so and odd_[so - 1].second > even_[se - 1].second)) { return odd_[--so].first; } else { return even_[--se].first; } } } pque; int _n; std::vector<int> head; std::vector<int> nexte; std::vector<Cap> caps; std::vector<int> rev; mf_pushrelabel(int n) : _n(n), head(n, -1), gap(-1) { 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 = rev.size(); rev.push_back(to), rev.push_back(from); caps.push_back(cap), caps.push_back(0); nexte.push_back(head[from]), head[from] = m; nexte.push_back(head[to]), head[to] = m + 1; return m / 2; } std::vector<int> dist; std::vector<int> dcnt; std::vector<Cap> excess; std::vector<int> q; int gap; void global_relabeling(int t) { dist.assign(_n, _n), dist[t] = 0; q.resize(_n); q[0] = t; int qb = 0, qe = 1; pque.clear(); if (UseGapRelabeling) gap = 1, dcnt.assign(_n + 1, 0); while (qb < qe) { int now = q[qb++]; if (UseGapRelabeling) gap = dist[now] + 1, dcnt[dist[now]]++; if (excess[now] > 0) pque.push(now, dist[now]); for (int e = head[now]; e >= 0; e = nexte[e]) { int nxt = rev[e]; if (caps[e ^ 1] and dist[nxt] == _n) dist[nxt] = dist[now] + 1, q[qe++] = nxt; } } } Cap flow(const int &s, const int &t) { pque.init(_n); assert(0 <= s and s < _n); assert(0 <= t and t < _n); assert(s != t); excess.assign(_n, 0); excess[s] = INF, excess[t] = -INF; dist.assign(_n, 0); dist[s] = _n; if (UseGapRelabeling) gap = 1, dcnt.assign(_n + 1, 0), dcnt[0] = _n - 1; for (int e = head[s]; e >= 0; e = nexte[e]) _push(s, e); if (UseGlobalRelabeling) global_relabeling(t); int tick = _n; while (!pque.empty()) { const int i = pque.pop(); if (UseGapRelabeling and dist[i] > gap) continue; int dnxt = _n * 2 - 1; for (int e = head[i]; e >= 0; e = nexte[e]) { if (!caps[e]) continue; int j = rev[e]; if (dist[j] == dist[i] - 1) { _push(i, e); if (excess[i] == 0) break; } else { if (dist[j] + 1 < dnxt) dnxt = dist[j] + 1; } } if (excess[i] > 0) { if (UseGapRelabeling) { if (dnxt != dist[i] and dcnt[dist[i]] == 1 and dist[i] < gap) gap = dist[i]; if (dnxt == gap) gap++; while (pque.highest() > gap) pque.pop(); if (dnxt > gap) dnxt = _n; if (dist[i] != dnxt) dcnt[dist[i]]--, dcnt[dnxt]++; } dist[i] = dnxt; if (!UseGapRelabeling or dist[i] < gap) pque.push(i, dist[i]); } if (UseGlobalRelabeling and --tick == 0) tick = _n, global_relabeling(t); } return excess.at(t) + INF; } void _push(int i, int e) { Cap delta = caps.at(e) < excess.at(i) ? caps.at(e) : excess.at(i); excess[i] -= delta, caps[e] -= delta; int j = rev[e]; excess[j] += delta, caps[e ^ 1] += delta; if (excess[j] > 0 and excess[j] <= delta) { if (!UseGapRelabeling or dist[j] <= gap) pque.push(j, dist[j]); } } }; #line 4 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp" #include <algorithm> #line 6 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp" #include <numeric> using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int H = rdi(), W = rdi(); vector<vector<int>> G(H, vector<int>(W)); for (auto &v : G) { for (auto &x : v) x = rdi(); } vector<int> R(H), C(W); long long tot = 0; for (auto &x : R) x = rdi(), tot += x; for (auto &x : C) x = rdi(), tot += x; int Z = 1 + H + W; mf_pushrelabel<long long, false, true, 1LL << 60> g(Z + 1); for (int i = 0; i < H; i++) { auto gtot = accumulate(G[i].begin(), G[i].end(), 0LL); auto f0 = min<long long>(gtot, R[i]); tot -= f0; if (gtot > f0) g.add_edge(0, i + 1, gtot - f0); for (int j = 0; j < W; j++) g.add_edge(i + 1, H + 1 + j, G[i][j]); } for (int j = 0; j < W; j++) g.add_edge(H + 1 + j, Z, C[j]); cout << tot - g.flow(0, Z) << '\n'; }