【洛谷4251】 [SCOI2015]小凸玩矩阵(二分答案,二分图匹配)

题面

传送门

Solution

看到什么最大值最小肯定二分啊。
check直接跑一个二分图匹配就好了。
orz ztl!!!

代码实现

/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=500010,M=2000010,Inf=1e9+10;
struct node
{
	int to,nxt,w;
}e[M<<1];
int front[N],cnt,s,t,dep[N],n,m,k,a[510][510];
void Add(int u,int v,int w)
{
	e[cnt]=(node){v,front[u],w};front[u]=cnt++;
	e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
void clear(){memset(front,-1,sizeof(front));cnt=0;}
queue<int>Q;
bool bfs()
{
	Q.push(s);memset(dep,0,sizeof(dep));
	dep[s]=1;
	while(!Q.empty())
	{
		int u=Q.front();Q.pop();
		for(int i=front[u];~i;i=e[i].nxt)
		{
			int v=e[i].to;
			if(e[i].w && !dep[v])
			{
				dep[v]=dep[u]+1;Q.push(v);
			}
		}
	}
	return dep[t];
}
int dfs(int u,int flow)
{
	if(u==t || !flow)return flow;
	for(int i=front[u];~i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(e[i].w && dep[v]==dep[u]+1)
		{
			int di=dfs(v,min(flow,e[i].w));
			if(di)
			{
				e[i].w-=di;e[i^1].w+=di;
				return di;
			}
			else dep[v]=0;
		}
	}
	return 0;
}
int dinic()
{
	int flow=0;
	while(bfs())
		while(int d=dfs(s,Inf))flow+=d;
	return flow;
}
void build(int mid)
{
	for(int i=1;i<=n;i++)
		Add(s,i,1);
	for(int i=1;i<=m;i++)
		Add(i+n,t,1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]<=mid)Add(i,j+n,1);
}
int main()
{
	n=gi();m=gi();k=gi();
	clear();int Max=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			a[i][j]=gi();
			Max=max(Max,a[i][j]);
		}
	int l=0,r=Max,ans=0;t=n+m+1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		clear();
		build(mid);
		if(dinic()>=n-k+1){r=mid-1;ans=mid;}
		else l=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}
posted @ 2019-03-20 22:25  QwQGJH  阅读(121)  评论(0编辑  收藏  举报