結果
| 問題 | No.957 植林 |
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-07-17 22:07:48 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 64 ms / 2,000 ms |
| コード長 | 17,040 bytes |
| 記録 | |
| コンパイル時間 | 7,602 ms |
| コンパイル使用メモリ | 153,056 KB |
| 最終ジャッジ日時 | 2025-01-11 22:43:57 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
#line 1 "main.cpp"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define PROBLEM "https://yukicoder.me/problems/no/957"
#line 2 "/Users/kodamankod/Desktop/Programming/Library/graph/network.cpp"
#include <cstddef>
#include <vector>
#include <numeric>
#include <utility>
template <class Edge>
class network {
public:
using vertex_type = typename Edge::vertex_type;
using edge_type = Edge;
using size_type = size_t;
protected:
std::vector<std::vector<edge_type>> M_graph;
public:
network() = default;
[[nodiscard]] vertex_type add_vertex() {
vertex_type res = M_graph.size();
M_graph.push_back({ });
return res;
}
[[nodiscard]] std::vector<vertex_type> add_vertices(const size_type size) {
size_type cur = M_graph.size();
std::vector<vertex_type> res(size);
std::iota(res.begin(), res.end(), cur);
M_graph.resize(cur + size);
return res;
}
void add_edge(const vertex_type src, const edge_type &edge) {
M_graph[src].push_back(edge);
}
template <class... Args>
void emplace_edge(const vertex_type src, Args&&... args) {
M_graph[src].emplace_back(std::forward<Args>(args)...);
}
const std::vector<std::vector<edge_type>> &get() const {
return M_graph;
}
size_type size() const {
return M_graph.size();
}
bool empty() const {
return M_graph.empty();
}
void clear() {
M_graph.clear();
M_graph.shrink_to_fit();
}
};
class base_edge {
public:
using vertex_type = size_t;
const vertex_type dest;
explicit base_edge(const vertex_type dest):
dest(dest)
{ }
};
template <class Flow>
class flow_edge: public base_edge {
public:
using vertex_type = typename base_edge::vertex_type;
using flow_type = Flow;
const flow_type capacity;
flow_type flow;
explicit flow_edge(const vertex_type dest, const flow_type capacity):
base_edge(dest), capacity(capacity), flow()
{ }
};
template <class Flow, class Cost>
class flow_cost_edge: public flow_edge<Flow> {
public:
using vertex_type = typename flow_edge<Flow>::vertex_type;
using flow_type = typename flow_edge<Flow>::flow_type;
using cost_type = Cost;
const cost_type cost;
explicit flow_cost_edge(const vertex_type dest, const flow_type capacity, const cost_type cost):
flow_edge<Flow>(dest, capacity), cost(cost)
{ }
};
/**
* @title Network
*/
#line 2 "/Users/kodamankod/Desktop/Programming/Library/graph/push_relabel.cpp"
#line 5 "/Users/kodamankod/Desktop/Programming/Library/graph/push_relabel.cpp"
#include <queue>
#include <algorithm>
#line 9 "/Users/kodamankod/Desktop/Programming/Library/graph/push_relabel.cpp"
#include <type_traits>
namespace push_relabel_detail {
class stack_helper {
private:
const size_t M_size;
std::vector<size_t> M_stack;
public:
explicit stack_helper(const size_t size):
M_size(size), M_stack(size * 2)
{ clear(); }
size_t top(const size_t height) const {
return M_stack[M_size + height];
}
bool empty(const size_t height) const {
return M_stack[M_size + height] == M_size + height;
}
void pop(const size_t height) {
M_stack[M_size + height] = M_stack[M_stack[M_size + height]];
}
void push(const size_t height, const size_t node) {
M_stack[node] = M_stack[M_size + height];
M_stack[M_size + height] = node;
}
void clear() {
std::iota(M_stack.begin() + M_size, M_stack.end(), M_size);
}
};
class list_helper {
private:
const size_t M_size;
std::vector<std::pair<size_t, size_t>> M_list;
public:
explicit list_helper(const size_t size):
M_size(size), M_list(size * 2)
{ clear(); }
bool empty(const size_t height) {
return M_list[M_size + height].second == M_size + height;
}
bool more_than_one(const size_t height) {
return M_list[M_size + height].first != M_list[M_size + height].second;
}
void insert(const size_t height, const size_t node) {
M_list[node].first = M_list[M_size + height].first;
M_list[node].second = M_size + height;
M_list[M_list[M_size + height].first].second = node;
M_list[M_size + height].first = node;
}
void erase(const size_t node) {
M_list[M_list[node].first].second = M_list[node].second;
M_list[M_list[node].second].first = M_list[node].first;
}
void clear() {
for (size_t index = M_size; index < M_size * 2; ++index) {
M_list[index].first = M_list[index].second = index;
}
}
void clear(const size_t height) {
const size_t index = M_size + height;
M_list[index].first = M_list[index].second = index;
}
template <class Func>
void apply_all(const size_t height, Func &&func) {
size_t index = M_list[M_size + height].second;
while (index < M_size) {
func(index);
index = M_list[index].second;
}
}
};
};
template <class Network>
class push_relabel {
public:
using network_type = Network;
using vertex_type = typename Network::vertex_type;
using edge_type = typename Network::edge_type;
using size_type = typename Network::size_type;
using flow_type = typename Network::edge_type::flow_type;
using height_type = size_t;
static_assert(std::is_integral<flow_type>::value, "invalid flow type :: non-integral");
private:
class residual_edge {
public:
const vertex_type dest;
flow_type remain;
const size_type rev;
const bool is_rev;
explicit residual_edge(const vertex_type dest, const flow_type remain,
const size_type rev, const bool is_rev):
dest(dest), remain(remain), rev(rev), is_rev(is_rev)
{ }
};
class node_type {
public:
std::vector<residual_edge> edges;
flow_type excess;
height_type height;
size_type iter;
node_type() = default;
};
residual_edge &M_cur_edge(const vertex_type node) {
return M_graph[node].edges[M_graph[node].iter];
}
residual_edge &M_rev_edge(const residual_edge &edge) {
return M_graph[edge.dest].edges[edge.rev];
}
void M_push(const vertex_type node, residual_edge &edge) {
auto flow = std::min(M_graph[node].excess, edge.remain);
edge.remain -= flow;
M_rev_edge(edge).remain += flow;
M_graph[node].excess -= flow;
M_graph[edge.dest].excess += flow;
}
void M_relabel(const vertex_type node) {
height_type min = M_graph.size() + 1;
for (const auto &edge: M_graph[node].edges) {
if (edge.remain > 0 && min > M_graph[edge.dest].height + 1) {
min = M_graph[edge.dest].height + 1;
}
}
M_graph[node].height = min;
}
std::vector<node_type> M_graph;
public:
push_relabel() = default;
explicit push_relabel(const network_type &net) {
const auto &graph = net.get();
M_graph.resize(graph.size());
for (size_type src = 0; src < graph.size(); ++src) {
for (const auto &edge: graph[src]) {
M_graph[src].edges.emplace_back(edge.dest, edge.capacity, M_graph[edge.dest].edges.size(), false);
M_graph[edge.dest].edges.emplace_back(src, 0, M_graph[src].edges.size() - 1, true);
}
}
}
flow_type max_flow(const vertex_type source, const vertex_type sink) {
push_relabel_detail::stack_helper active(M_graph.size());
push_relabel_detail::list_helper level(M_graph.size());
height_type min_gap, max_active;
{
for (auto &node: M_graph) {
node.excess = 0;
node.height = M_graph.size() + 1;
node.iter = 0;
for (auto &edge: node.edges) {
if (edge.is_rev) edge.remain = 0;
else edge.remain = edge.remain + M_rev_edge(edge).remain;
}
}
M_graph[sink].height = 0;
std::queue<vertex_type> queue;
queue.push(sink);
while (!queue.empty()) {
const auto node = queue.front();
queue.pop();
for (const auto &edge: M_graph[node].edges) {
if (M_rev_edge(edge).remain > 0) {
if (M_graph[edge.dest].height == M_graph.size() + 1) {
M_graph[edge.dest].height = M_graph[node].height + 1;
queue.push(edge.dest);
}
}
}
}
if (M_graph[source].height == M_graph.size() + 1) {
return 0;
}
for (auto &edge: M_graph[source].edges) {
M_graph[source].excess += edge.remain;
M_push(source, edge);
}
M_graph[source].height = M_graph.size();
min_gap = M_graph.size();
max_active = 0;
for (size_type index = 0; index < M_graph.size(); ++index) {
const auto &node = M_graph[index];
if (node.height < M_graph.size()) {
if (node.excess > 0 && index != sink) {
active.push(node.height, index);
max_active = std::max(max_active, node.height);
}
level.insert(node.height, index);
}
}
for (size_type index = 0; index < M_graph.size(); ++index) {
if (level.empty(index)) {
min_gap = index;
break;
}
}
}
while (max_active > 0) {
if (active.empty(max_active)) {
--max_active;
continue;
}
const auto node = active.top(max_active);
active.pop(max_active);
while (true) {
auto &edge = M_cur_edge(node);
if (edge.remain > 0 && M_graph[node].height == M_graph[edge.dest].height + 1) {
if (M_graph[edge.dest].excess == 0 && edge.dest != sink) {
active.push(M_graph[edge.dest].height, edge.dest);
max_active = std::max(max_active, M_graph[edge.dest].height);
}
M_push(node, edge);
if (M_graph[node].excess == 0) {
break;
}
}
M_graph[node].iter++;
if (M_graph[node].iter == M_graph[node].edges.size()) {
M_graph[node].iter = 0;
if (level.more_than_one(M_graph[node].height)) {
level.erase(node);
M_relabel(node);
if (M_graph[node].height > min_gap) {
M_graph[node].height = M_graph.size() + 1;
break;
}
if (M_graph[node].height == min_gap) {
++min_gap;
}
level.insert(M_graph[node].height, node);
}
else {
for (height_type index = M_graph[node].height; index < min_gap; ++index) {
level.apply_all(index, [&](const vertex_type tmp) {
M_graph[tmp].height = M_graph.size() + 1;
});
level.clear(index);
}
break;
}
}
}
max_active = std::min(max_active, min_gap - 1);
}
return M_graph[sink].excess;
}
};
/**
* @title Push Relabel
*/
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/fast_io.cpp"
#line 4 "/Users/kodamankod/Desktop/Programming/Library/other/fast_io.cpp"
#include <cstdint>
#include <cstring>
#include <iostream>
namespace fast_io {
static constexpr size_t buf_size = 1 << 18;
static constexpr size_t buf_margin = 1;
static constexpr size_t block_size = 10000;
static constexpr size_t integer_size = 20;
static char inbuf[buf_size + buf_margin] = {};
static char outbuf[buf_size + buf_margin] = {};
static char block_str[block_size * 4 + buf_margin] = {};
static constexpr uint64_t power10[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000,
100000000000000, 1000000000000000, 10000000000000000, 100000000000000000,
1000000000000000000, 10000000000000000000u
};
class scanner {
private:
size_t M_in_pos = 0, M_in_end = buf_size;
void M_load() {
M_in_end = fread(inbuf, 1, buf_size, stdin);
inbuf[M_in_end] = '\0';
}
void M_reload() {
size_t length = M_in_end - M_in_pos;
memmove(inbuf, inbuf + M_in_pos, length);
M_in_end = length + fread(inbuf + length, 1, buf_size - length, stdin);
inbuf[M_in_end] = '\0';
M_in_pos = 0;
}
void M_ignore_space() {
while (inbuf[M_in_pos] <= ' ') {
if (__builtin_expect(++M_in_pos == M_in_end, 0)) M_reload();
}
}
char M_next() { return inbuf[M_in_pos++]; }
char M_next_nonspace() {
M_ignore_space();
return inbuf[M_in_pos++];
}
public:
scanner() { M_load(); }
void scan(char &c) { c = M_next_nonspace(); }
void scan(std::string &s) {
M_ignore_space();
s = "";
do {
size_t start = M_in_pos;
while (inbuf[M_in_pos] > ' ') ++M_in_pos;
s += std::string(inbuf + start, inbuf + M_in_pos);
if (inbuf[M_in_pos] != '\0') break;
M_reload();
} while (true);
}
template <class T>
typename std::enable_if<std::is_integral<T>::value, void>::type scan(T &x) {
char c = M_next_nonspace();
if (__builtin_expect(M_in_pos + integer_size >= M_in_end, 0)) M_reload();
bool n = false;
if (c == '-') n = true, x = 0;
else x = c & 15;
while ((c = M_next()) >= '0') x = x * 10 + (c & 15);
if (n) x = -x;
}
template <class T, class... Args>
void scan(T &x, Args&... args) {
scan(x); scan(args...);
}
template <class T>
scanner& operator >> (T &x) {
scan(x); return *this;
}
};
class printer {
private:
size_t M_out_pos = 0;
void M_flush() {
fwrite(outbuf, 1, M_out_pos, stdout);
M_out_pos = 0;
}
void M_precompute() {
for (size_t i = 0; i < block_size; ++i) {
size_t j = 4, k = i;
while (j--) {
block_str[i * 4 + j] = k % 10 + '0';
k /= 10;
}
}
}
static constexpr size_t S_integer_digits(uint64_t n) {
if (n >= power10[10]) {
if (n >= power10[19]) return 20;
if (n >= power10[18]) return 19;
if (n >= power10[17]) return 18;
if (n >= power10[16]) return 17;
if (n >= power10[15]) return 16;
if (n >= power10[14]) return 15;
if (n >= power10[13]) return 14;
if (n >= power10[12]) return 13;
if (n >= power10[11]) return 12;
return 11;
}
else {
if (n >= power10[9]) return 10;
if (n >= power10[8]) return 9;
if (n >= power10[7]) return 8;
if (n >= power10[6]) return 7;
if (n >= power10[5]) return 6;
if (n >= power10[4]) return 5;
if (n >= power10[3]) return 4;
if (n >= power10[2]) return 3;
if (n >= power10[1]) return 2;
return 1;
}
}
public:
printer() { M_precompute(); }
~printer() { M_flush(); }
void print(char c) {
outbuf[M_out_pos++] = c;
if (__builtin_expect(M_out_pos == buf_size, 0)) M_flush();
}
void print(const char *s) {
while (*s != 0) {
outbuf[M_out_pos++] = *s++;
if (M_out_pos == buf_size) M_flush();
}
}
void print(const std::string &s) {
for (auto c: s) {
outbuf[M_out_pos++] = c;
if (M_out_pos == buf_size) M_flush();
}
}
template <class T>
typename std::enable_if<std::is_integral<T>::value, void>::type print(T x) {
if (__builtin_expect(M_out_pos + integer_size >= buf_size, 0)) M_flush();
if (x < 0) print('-'), x = -x;
size_t digit = S_integer_digits(x);
size_t len = digit;
while (len >= 4) {
len -= 4;
memcpy(outbuf + M_out_pos + len, block_str + (x % block_size) * 4, 4);
x /= 10000;
}
memcpy(outbuf + M_out_pos, block_str + x * 4 + 4 - len, len);
M_out_pos += digit;
}
template <class T, class... Args>
void print(const T &x, const Args&... args) {
print(x); print(' '); print(args...);
}
template <class... Args>
void println(const Args&... args) {
print(args...); print('\n');
}
template <class T>
printer& operator << (const T &x) {
print(x); return *this;
}
};
};
/**
* @title Fast Input/Output
*/
#line 10 "main.cpp"
#line 13 "main.cpp"
fast_io::scanner cin;
fast_io::printer cout;
int main() {
size_t H, W;
cin.scan(H, W);
network<flow_edge<int64_t>> graph;
const auto source = graph.add_vertex();
const auto sink = graph.add_vertex();
const auto row = graph.add_vertices(H);
const auto column = graph.add_vertices(W);
std::vector<int64_t> accum(H);
for (size_t i = 0; i < H; ++i) {
for (size_t j = 0; j < W; ++j) {
int32_t g;
cin.scan(g);
accum[i] += g;
graph.emplace_edge(row[i], column[j], g);
}
}
int64_t sum = 0;
for (size_t i = 0; i < H; ++i) {
int64_t r;
cin.scan(r);
int64_t min = std::min(accum[i], r);
sum += r - min;
graph.emplace_edge(source, row[i], accum[i] - min);
}
for (size_t j = 0; j < W; ++j) {
int64_t r;
cin.scan(r);
sum += r;
graph.emplace_edge(column[j], sink, r);
}
cout.println(sum - push_relabel(graph).max_flow(source, sink));
return 0;
}
KoD