文理分科题解
裁剪自我的blog
这是一个思想的新大陆,这使 OI 世界的观念逐渐建立起来,这促进 OI 主义的发展,这体现了 OI(€€£) 原始资本积累的残酷性……( whk 选手考完定(mei)时(yue)作(kao)业(shi) 的大脑。
以上都是屁话。 不过确实是一个新思路。
题解部分:
ps:由于画图不好画有向边,这里的边的方向都是从左(源点)到右(汇点), 看不懂也没办法了。
首先把找最大贡献改为去掉最少的贡献。
还是先用最小割的套路,连接 s s s 的边断了表示选择理科,连接 t t t 的边断了表示选择文科。
所以 s → a s \rightarrow a s→a 的边权为 a r t [ a ] art[a] art[a] (选择理科就没有选择文科的贡献), a → t a \rightarrow t a→t 的边权为 s c i e n c e [ a ] science[a] science[a](同理)。
如图(a[1~4]表示a的相邻点)。
现在考虑怎么连边可以处理需要去掉全为理科(或文科)的贡献。
拿全选理科的贡献举例。
相当于当有一条 a[0~4] → \rightarrow → t的边没有被断掉,那么表示五个人中有人选择了文科,这时就需要减去全选理科的贡献。
考虑如下图的连边。
如果没断掉 a [ i ] → t a[i] \rightarrow t a[i]→t,那么相当于 a [ i ] a[i] a[i] 选择了文科,那么这时有两条增广路 s → a [ i ] → a ′ → t s \rightarrow a[i] \rightarrow a' \rightarrow t s→a[i]→a′→t, s → a ′ ′ → a → a ′ → t s \rightarrow a'' \rightarrow a \rightarrow a' \rightarrow t s→a′′→a→a′→t,为了破坏这两条增广路,我们发现只能选择
- a ′ → t a' \rightarrow t a′→t,这种情况的贡献为 same_science[a]
- s → a 和 s → a ′ s \rightarrow a 和 s \rightarrow a' s→a和s→a′,这种情况的贡献为 same_art[a],art[a],可是总贡献为 same_art[a],art[a],science[a]。不如 art[a],same_art[a],所以(最小割)肯定不会选择这样断边。
- 其他的边,都为 Inf 肯定不选了。
所以这个构造满足了所有条件(妙啊。
代码非常非常简单……麻了
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const LL Mod = 1e9 + 7;
const int Maxt = 100;
const int Maxn = 2 * 1e5;
const int Maxm = 1e7;
const LL Inf = 0x3f3f3f3f;
void del (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void add (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }
struct edge {
int to[Maxm * 2 + 5], Next[Maxm * 2 + 5]; LL val[Maxm * 2 + 5];
int len, Head[Maxn + 5];
edge () { len = 1; memset (Head, 0, sizeof Head); }
void Init () {
len = 1;
memset (Head, 0, sizeof Head);
}
void plus (int x, int y, LL w) {
to[++len] = y;
Next[len] = Head[x];
val[len] = w;
Head[x] = len;
}
void add (int x, int y, LL w) {
plus (x, y, w); plus (y, x, 0);
}
}mp;
int s, t;
int hh, tt, q[Maxn + 5];
int depth[Maxn + 5], cur[Maxn + 5];
bool BFS () {
rep (i, 1, tt) depth[q[i]] = 0;
hh = 1; tt = 0; q[++tt] = s;
depth[s] = 1; cur[s] = mp.Head[s];
while (hh <= tt) {
int u = q[hh++];
for (int i = mp.Head[u]; i; i = mp.Next[i]) {
int v = mp.to[i]; LL w = mp.val[i];
if (w == 0) continue;
if (depth[v]) continue;
depth[v] = depth[u] + 1;
cur[v] = mp.Head[v];
q[++tt] = v;
if (v == t) return 1;
}
}
return 0;
}
LL DFS (int u, LL Up) {
if (u == t) return Up;
if (Up == 0) return 0;
LL flow = 0;
for (int i = cur[u]; i && Up != flow; i = mp.Next[i]) {
int v = mp.to[i]; LL w = mp.val[i];
cur[u] = i;
if (w == 0) continue;
if (depth[v] != depth[u] + 1) continue;
LL tmp = DFS (v, Min (Up - flow, w));
if (tmp == 0) depth[v] = -1;
flow += tmp; mp.val[i] -= tmp; mp.val[i ^ 1] += tmp;
}
return flow;
}
LL Dinic () {
LL flow = 0;
while (BFS ()) {
flow += DFS (s, Inf);
}
return flow;
}
int n, m;
int art[Maxt + 5][Maxt + 5];
int science[Maxt + 5][Maxt + 5];
int same_art[Maxt + 5][Maxt + 5];
int same_science[Maxt + 5][Maxt + 5];
int tox[10] = { 0, 1, -1, 0, 0, 0 };
int toy[10] = { 0, 0, 0, 1, -1, 0 };
int Hash (int x, int y) {
return (x - 1) * m + y;
}
bool check (int x, int y) {
if (x < 1 || x > n) return 0;
if (y < 1 || y > m) return 0;
return 1;
}
int main () {
// freopen ("D:\\lihan\\1.in", "r", stdin);
// freopen ("D:\\lihan\\1.out", "w", stdout);
read (n, m);
int tot = 0;
rep (i, 1, n) rep (j, 1, m) read (art[i][j]), tot += art[i][j];
rep (i, 1, n) rep (j, 1, m) read (science[i][j]), tot += science[i][j];
rep (i, 1, n) rep (j, 1, m) read (same_art[i][j]), tot += same_art[i][j];
rep (i, 1, n) rep (j, 1, m) read (same_science[i][j]), tot += same_science[i][j];
s = 0; t = Maxn - 1;
rep (i, 1, n) {
rep (j, 1, m) {
mp.add (s, Hash (i, j), art[i][j]);
mp.add (Hash (i, j), t, science[i][j]);
mp.add (s, Hash (i, j) + n * m, same_art[i][j]);
mp.add (Hash (i, j) + n * m * 2, t, same_science[i][j]);
rep (k, 1, 5) {
int x = i + tox[k], y = j + toy[k];
if (!check (x, y)) continue;
mp.add (Hash (i, j) + n * m, Hash (x, y), Inf);
mp.add (Hash (x, y), Hash (i, j) + n * m * 2, Inf);
}
}
}
print (tot - Dinic (), '\n');
return 0;
}