結果
| 問題 | No.957 植林 |
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-08-27 01:43:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 72 ms / 2,000 ms |
| コード長 | 6,363 bytes |
| 記録 | |
| コンパイル時間 | 1,290 ms |
| コンパイル使用メモリ | 93,644 KB |
| 最終ジャッジ日時 | 2025-01-24 02:12:31 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
#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 <limits>
#include <utility>
#include <vector>
// Maxflow (push-relabel, highest-label)
// Complexity: O(N^2 M^(1/2))
template <class Cap, Cap INF = std::numeric_limits<Cap>::max() / 2, bool UseGlobalRelabeling = true, bool UseGapRelabeling = true>
struct mf_pushrelabel {
struct pque_ {
std::vector<std::pair<int, int>> even_, odd_;
int se, so;
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;
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<int> dcnt;
std::vector<Cap> excess;
std::vector<int> q;
int gap;
void global_relabeling(int t) {
dist.assign(_n, _n), dist[t] = 0;
if (q.empty()) 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 (const auto &e : g[now]) {
if (g[e.to][e.rev].cap and dist[e.to] == _n) {
dist[e.to] = dist[now] + 1;
q[qe++] = e.to;
}
}
}
}
Cap flow(const int &s, const int &t) {
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;
pque.init(_n);
for (auto &e : g[s]) push(s, e);
if (UseGapRelabeling) gap = 1, dcnt.assign(_n + 1, 0), dcnt[0] = _n - 1;
if (UseGlobalRelabeling) global_relabeling(t);
int tick = _n;
while (!pque.empty()) {
int i = pque.pop();
if (UseGapRelabeling and dist[i] > gap) continue;
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) {
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[t] + INF;
}
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) {
if (!UseGapRelabeling or dist[e.to] <= gap) pque.push(e.to, dist[e.to]);
}
}
};
#line 4 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp"
#include <algorithm>
#include <iostream>
#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, 1LL << 60, false, true> 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';
}
hitonanode