水题笔记_ 士兵占领 [网络流, 清奇脑回路]

正难则反

懵逼的 题目

扯淡的 题解

好像是GTY爷爷讲的来着…好像课件还出锅了来着

考虑到这个题是求最小直接肛不好搞啊, 我们就考虑转化问题, 把[放入的士兵最少]转化为[不放的士兵最多]

这样就是一个裸的二分图匹配了…容量代表这一 行/列 最多能不放多少士兵, 还能满足约束条件

  • $S \to x_i\,\,cap = m - need_{x_i} - cnt_{x_i}$
  • $y_i \to T\,\, cap = n - need_{y_i} - cnt_{y_i}$
  • $x_i \to y_i\,\, cap = 1\,\, Hinder_{x, y} = false$

最后输出n * m - k - ans即可

沙茶的 代码

Emmmmm 发掘了新的错法…b[rev(i)].flow += zl…Emmmmm…

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
/**************************************************************
Problem: 1458
User: Cansult
Language: C++
Result: Accepted
Time:48 ms
Memory:21856 kb
****************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define MAXN (100000 + 5)
#define MAXM (500000 + 5)
#define INF (0x7fffffff)
#define rev(a) ((a) ^ 1)
#define MAXL (500)
#define bh(i) ((i) + MAXL)
const int s = 0, t = MAXN - 1;
using namespace std;
struct edg
{
int from, to, next, cap, flow;
edg() {}
edg(int fr, int dqt, int ne, int ca): from(fr), to(dqt), next(ne), cap(ca), flow(0) {}
}b[MAXM << 1];
int g[MAXN], cntb = -1, dis[MAXN], n, m, cntx[MAXL], cnty[MAXL], totso, needx[MAXL], needy[MAXL], ans, k;
bool iss[MAXL][MAXL];
void adn(int from, int to, int cap)
{
b[++cntb] = edg(from, to, g[from], cap);
g[from] = cntb;
}
bool bfs()
{
memset(dis, 0, sizeof(dis));
queue<int> q;
q.push(s);
dis[0] = 1;
while (!q.empty())
{
int dq = q.front();
q.pop();
for (int i = g[dq]; ~i; i = b[i].next)
if (b[i].cap > b[i].flow && !dis[b[i].to])
dis[b[i].to] = dis[dq] + 1, q.push(b[i].to);
}
return dis[t] > 0;
}
int dinic(int dq, int maxf)
{
if (dq == t || !maxf)
return maxf;
int re = 0;
for (int i = g[dq]; ~i; i = b[i].next)
if (b[i].cap > b[i].flow && dis[b[i].to] == dis[dq] + 1)
{
int zl = dinic(b[i].to, min(maxf, b[i].cap - b[i].flow));
re += zl;
b[i].flow += zl;
b[rev(i)].flow -= zl;
maxf -= zl;
}
return re;
}
void solve()
{
while (bfs())
ans += dinic(s, INF);
}
int main()
{
memset(g, -1, sizeof(g));
memset(iss, false, sizeof(iss));
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++)
scanf("%d", &needy[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &needx[i]);
for (int i = 1, x, y; i <= k; i++)
scanf("%d%d", &x, &y), ++cnty[y], ++cntx[x], iss[x][y] = true;
for (int i = 1; i <= n; i++)
adn(s, i, m - cntx[i] - needx[i]), adn(i, s, 0);
for (int i = 1; i <= m; i++)
adn(bh(i), t, n - cnty[i] - needy[i]), adn(t, bh(i), 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!iss[i][j])
adn(i, bh(j), 1), adn(bh(j), i, 0);
totso = n * m - k;
solve();
printf("%d", totso - ans);
return 0;
}


/*
4 4 4
1 1 1 1
0 1 0 3
1 4
2 2
3 3
4 3
*/

感觉最近好颓啊…净刷水了…

By 三分钟热度的 Cansult