题面
解法
显然答案满足单调性,直接二分答案\(mid\)
考虑建一个类似于二分图的东西
因为每一行每一列只能被使用一次,所以拆点,容量为1
若\(a_{i,j}≤mid\),那么在\(i\)和\(j\)相连,容量为1
限制整张网络容量为\(n-k+1\)
如果最大流为\(n-k+1\),那么显然答案可行
其实不用拆点的,我是sb
代码
#include <bits/stdc++.h>
#define N 300
using namespace std;
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Edge {
int next, num, c;
} e[N * N * 16];
int n, m, k, s, t, ss, cnt, l[N * 4], cur[N * 4], a[N][N];
void add(int x, int y, int c) {
e[++cnt] = (Edge) {e[x].next, y, c};
e[x].next = cnt;
}
void Add(int x, int y, int c) {
add(x, y, c), add(y, x, 0);
}
bool bfs(int s) {
for (int i = 1; i <= ss; i++) l[i] = -1;
queue <int> q; q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int p = e[x].next; p; p = e[p].next) {
int k = e[p].num, c = e[p].c;
if (c && l[k] == -1)
l[k] = l[x] + 1, q.push(k);
}
}
return l[t] != -1;
}
int dfs(int x, int lim) {
if (x == t) return lim;
int used = 0;
for (int p = cur[x]; p; p = e[p].next) {
int k = e[p].num, c = e[p].c;
if (l[k] == l[x] + 1 && c) {
int w = dfs(k, min(c, lim - used));
e[p].c -= w, e[p ^ 1].c += w;
if (e[p].c) cur[x] = p; used += w;
if (used == lim) return lim;
}
}
if (!used) l[x] = -1;
return used;
}
int dinic() {
int ret = 0;
while (bfs(s)) {
for (int i = s; i <= ss; i++)
cur[i] = e[i].next;
ret += dfs(s, INT_MAX);
}
return ret;
}
bool check(int mid) {
s = 0, t = 2 * (n + m) + 1, ss = cnt = t + 1;
if (cnt % 2 == 0) cnt++;
for (int i = 0; i <= ss; i++) e[i].next = 0;
Add(s, ss, n - k + 1);
for (int i = 1; i <= n; i++)
Add(i * 2 - 1, i * 2, 1), Add(ss, i * 2 - 1, 1);
for (int i = 1; i <= m; i++)
Add(2 * n + 2 * i - 1, 2 * n + 2 * i, 1), Add(2 * (n + i), t, 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] <= mid) Add(i * 2, 2 * n + 2 * j - 1, 1);
int tx = dinic();
return tx == n - k + 1;
}
int main() {
read(n), read(m), read(k);
int l = INT_MAX, r = -l, ans;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
read(a[i][j]), l = min(l, a[i][j]), r = max(r, a[i][j]);
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << "\n";
return 0;
}