-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdinic2.cpp
146 lines (132 loc) · 3.88 KB
/
dinic2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// added
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Dinic {
struct edge {
int to, cap, rev;
};
int n;
vector<vector<edge>> G;
vector<int> level;
vector<int> iter;
void bfs(int s) {
level.assign(n, -1);
queue<int> que;
que.push(s);
level[s] = 0;
while (que.size()) {
int v = que.front();
que.pop();
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f) {
if (v == t) return f;
for (int i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
Dinic(int n) : n(n), G(n), level(n), iter(n) {}
void add_edge(int from, int to, int cap) {
G[from].push_back({to, cap, (int) G[to].size()});
G[to].push_back({from, 0, (int) G[from].size() - 1});
}
int max_flow(int s, int t) {
int flow = 0;
while (1) {
bfs(s);
if (level[t] < 0) return flow;
iter.assign(n, 0);
int f;
while ((f = dfs(s, t, 1e9)) > 0) {
flow += f;
}
}
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w, W;
cin >> h >> w >> W;
vector<vector<int>> be(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> be[i][j];
}
}
vector<int> as, bs, cs;
auto push = [&](int a, int b, int c) {
as.push_back(a);
bs.push_back(b);
cs.push_back(c);
};
int idx = 0;
map<pair<int, int>, int> mp;
constexpr int T = 100000, S = 200000;
int dy[] = {-1, 1};
auto get_mid = [&](int i, int j, int d) {
return be[i][j + d];
};
int sum = 0;
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < w - 1; j++) {
if ((i + j) & 1) continue;
mp[{i, j}] = idx;
int B = be[i][j] + be[i + 1][j] + be[i][j + 1] + be[i + 1][j + 1];
sum += B;
if (i & 1) {
push(S, idx, W);
push(idx, T, B);
} else {
push(S, idx, B);
push(idx, T, W);
}
for (int k = 0; k < 2; k++) {
int x = i - 1;
int y = j + dy[k];
if (0 <= x && 0 <= y && y < w - 1) {
// cerr << "mp" << endl;
// for (auto &p : mp) {
// cerr << "(" << p.first.first << "," << p.first.second << ")" << endl;
// }
// cerr << "cur" << endl;
// cerr << "(" << i << "," << j << "), (" << x << "," << y << ")" << endl;
assert(mp.count({x, y}));
int mid = get_mid(i, j, k);
if (x & 1) {
push(idx, mp[{x, y}], mid);
} else {
push(mp[{x, y}], idx, mid);
}
}
}
idx++;
}
}
Dinic dinic(idx + 2);
for (int i = 0; i < as.size(); i++) {
int a = as[i], b = bs[i], c = cs[i];
if (a == S) a = idx;
if (b == T) b = idx + 1;
// cout << "edge: " << a << ' ' << b << ' ' << c << endl;
dinic.add_edge(a, b, c);
}
cout << sum - dinic.max_flow(idx, idx + 1) << endl;
}