「SCOI2015」小凸玩矩阵

「SCOI2015」小凸玩矩阵

传送门
首先转化一下题意:求第 \(k\) 大的最小值,就是求第 \(n - k + 1\) 小的最大值。
然后我们就可以二分这个值 \(mid\),然后把网格图上 $\le mid $ 的点看作1,其他的看作0,判断最大流是否 \(\ge n - k + 1\),然后调整左右端点。
参考代码:

#include <cstring>
#include <cstdio>
#include <queue>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while ('0' > c || c > '9') f |= c == '-', c = getchar();
	while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
	s = f ? -s : s;
}

const int _ = 250 * 2 + 10, __ = 250 * 250 + 250 * 2 + 10, INF = 2147483647;

int tot = 1, head[_], nxt[__ << 1], ver[__ << 1], cap[__ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, cap[tot] = d; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, 0);}

int n, m, k, a[252][252], s, t, dep[_];

inline int bfs() {
	queue < int > Q;
	while (!Q.empty()) Q.pop();
	memset(dep, 0, sizeof dep);
	Q.push(s), dep[s] = 1;
	while (!Q.empty()) {
		int u = Q.front(); Q.pop();
		for (rg int i = head[u]; i; i = nxt[i]) {
			int v = ver[i];
			if (dep[v] == 0 && cap[i] > 0)
				dep[v] = dep[u] + 1, Q.push(v);
		}
	}
	return dep[t] > 0;
}

inline int dfs(int u, int flow) {
	if (u == t) return flow;
	for (rg int i = head[u]; i; i = nxt[i]) {
		int v = ver[i];
		if (dep[v] == dep[u] + 1 && cap[i] > 0) {
			int res = dfs(v, min(flow, cap[i]));
			if (res) { cap[i] -= res, cap[i ^ 1] += res; return res; }
		}
	}
	return 0;
}

inline int Dinic() {
	int res = 0;
	while (bfs()) while (int d = dfs(s, INF)) res += d;
	return res;
}

inline bool check(int mid) {
	tot = 1, memset(head, 0, sizeof head);
	for (rg int i = 1; i <= n; ++i) link(s, i, 1);
	for (rg int j = 1; j <= m; ++j) link(j + n, t, 1);
	for (rg int i = 1; i <= n; ++i)
		for (rg int j = 1; j <= m; ++j)
			if (a[i][j] <= mid) link(i, j + n, 1);
	return Dinic() >= n - k + 1;
}

int main() {
#ifndef ONLINE_JUDGE
	file("cpp");
#endif
	read(n), read(m), read(k);
	for (rg int i = 1; i <= n; ++i)
		for (rg int j = 1; j <= m; ++j) read(a[i][j]);
	s = 0, t = n + m + 1;
	int l = 1, r = 1e9;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (check(mid)) r = mid; else l = mid + 1;
	}
	printf("%d\n", l);
	return 0;
}
posted @ 2020-01-14 10:20  Sangber  阅读(152)  评论(0编辑  收藏  举报