4443: [Scoi2015]小凸玩矩阵

4443: [Scoi2015]小凸玩矩阵

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 521   Solved: 259
[ Submit][ Status][ Discuss]

Description

小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或同一列,现小凸想知道选出来的N个数中第K大的数字的最小值是多少。

Input

第一行给出三个整数N,M,K
接下来N行,每行M个数字,用来描述这个矩阵

Output

如题 

Sample Input

3 4 2
1 5 6 6
8 3 4 3
6 8 6 3

Sample Output

3

HINT

1<=K<=N<=M<=250,1<=矩阵元素<=10^9


Source

[ Submit][ Status][ Discuss]

二分ans,二分图匹配是否能找到n - k + 1个小于等于ans的数
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 300;
const int INF = 1E9 + 10;

struct E{
	int to,cap,flow;
	E(int _to = 0,int _cap = 0,int _flow = 0) {to = _to; cap = _cap; flow = _flow;}
}edgs[maxn*maxn*20];

int n,m,cnt,Cnt,K,S,T,num[maxn][maxn],cur[maxn*2],L[maxn*2],vis[maxn*2];

vector <int> v[maxn*2];
queue <int> Q;

void Add(int x,int y,int w)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,w,0);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}

bool BFS()
{
	vis[S] = ++Cnt; L[S] = 1; Q.push(S);
	while (!Q.empty()) {
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++) {
			E e = edgs[v[k][i]];
			if (e.cap == e.flow) continue;
			if (vis[e.to] == Cnt) continue;
			L[e.to] = L[k] + 1;
			vis[e.to] = Cnt;
			Q.push(e.to);
 		}
	}
	return vis[T] == Cnt;
}

int Dicnic(int x,int a)
{
	if (x == T || !a) return a;
	int flow = 0;
	for (int &i = cur[x]; i < v[x].size(); i++) {
		E &e = edgs[v[x][i]];
		if (e.cap == e.flow) continue;
		if (L[e.to] != L[x] + 1) continue;
		int f = Dicnic(e.to,min(a,e.cap - e.flow));
		if (!f) continue;
		e.flow += f;
		edgs[v[x][i]^1].flow -= f;
		a -= f;
		flow += f;
		if (!a) return flow;
	}
	if (!flow) L[x] = -1;
	return flow;
}

bool Judge(int now)
{
	cnt = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (num[i][j] <= now)
				Add(i,n+j,INF);
	for (int i = 1; i <= n; i++) Add(S,i,1);
	for (int i = 1; i <= m; i++) Add(n+i,T,1);
	int MaxFlow = 0;
	while (BFS()) {
		for (int i = S; i <= T; i++) cur[i] = 0;
		MaxFlow += Dicnic(S,INF);
	}
	for (int i = S; i <= T; i++) v[i].clear();
	return MaxFlow >= n - K + 1;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m >> K;
	S = 0; T = n + m + 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			scanf("%d",&num[i][j]);
	int l = 0,r = INF;
	while (r - l > 1) {
		int mid = (l + r) >> 1;
		if (Judge(mid)) r = mid;
		else l = mid;
	}
	if (Judge(l)) cout << l; else cout << r;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值