看到这种第(k)大的题面就想到二分答案对吧,问题在于二分后怎么(check)。直接网络流跑就行了。
#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 re register
#define ll long long
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=5010,Inf=1e9+10;
int n,m,k,a[N][N],front[N<<1],cnt,s,t,dep[N];
struct node{int to,nxt,w;}e[N*N<<1];
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 build(int mid){
memset(front,-1,sizeof(front));cnt=0;s=0;t=n+m+1;
for(int i=1;i<=n;i++)Add(s,i,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);
for(int j=1;j<=m;j++)Add(j+n,t,1);
}
queue<int>Q;
bool bfs(){
memset(dep,0,sizeof(dep));dep[s]=1;
Q.push(s);
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(e[i].w,flow));
if(di){e[i].w-=di;e[i^1].w+=di;return di;}
else dep[v]=0;
}
}
return 0;
}
int dinic(){
int fl=0;
while(bfs())
while(int d=dfs(s,Inf))fl+=d;
return fl;
}
bool check(int mid){
build(mid);
return dinic()>=n-k+1;
}
int main(){
n=gi();m=gi();k=gi();int l=1e9+10,r=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
a[i][j]=gi(),l=min(l,a[i][j]),r=max(r,a[i][j]);
int ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){ans=mid;r=mid-1;}
else l=mid+1;
}
printf("%d
",ans);
return 0;
}