結果
| 問題 | No.957 植林 |
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-08-25 22:00:41 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 179 ms / 2,000 ms |
| コード長 | 4,496 bytes |
| 記録 | |
| コンパイル時間 | 1,305 ms |
| コンパイル使用メモリ | 93,716 KB |
| 最終ジャッジ日時 | 2025-01-24 01:51:21 |
|
ジャッジサーバーID (参考情報) |
judge1 / 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 "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 UseGlobalLabeling = false>
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];
}
void global_labeling(int t) {
if (!UseGlobalLabeling) return;
dist.assign(_n, _n), dist[t] = 0;
static std::vector<int> q;
q = {t};
int qh = 0;
h2v.assign(_n * 2, {});
max_height = -1;
while (qh < int(q.size())) {
int now = q[qh++];
if (excess[now] > 0) activate(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.push_back(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;
h2v.assign(_n * 2, {});
max_height = -1;
for (auto &e : g[s]) push(s, e);
global_labeling(t);
int tick = _n;
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);
}
if (--tick == 0) tick = _n, global_labeling(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) activate(e.to);
}
};
#line 3 "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, W;
cin >> H >> W;
vector<vector<int>> G(H, vector<int>(W));
for (auto &v : G) {
for (auto &x : v) cin >> x;
}
vector<int> R(H), C(W);
for (auto &x : R) cin >> x;
for (auto &x : C) cin >> x;
long long tot = accumulate(R.begin(), R.end(), 0LL) + accumulate(C.begin(), C.end(), 0LL);
long long f1 = 0;
int Z = 1 + H + W;
mf_pushrelabel<long long> 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]);
f1 += 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 - f1 - g.flow(0, Z) << '\n';
}
hitonanode