行列看成点,格子看成边,二分一个边权,删去大于它的边,新图上的最大流>k则答案可以更优,小于k则调整左边界。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std; inline int rd(){
int ret=,f=;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-:;
while(isdigit(c))ret=ret*+c-'',c=getchar();
return ret*f;
} const int MAXN=*;
const int M=**; struct Edge{
int next,to,f;
}e[M<<];
int ecnt=,head[MAXN];
inline void add(int x,int y,int f){
e[++ecnt].next = head[x];
e[ecnt].to = y;
e[ecnt].f = f;
head[x] = ecnt;
} int n,m,num;
int a[][];
int b[MAXN],p; int dep[MAXN];
queue<int> Q;
bool bfs(int s,int t){
memset(dep,,sizeof(dep));
dep[s]=;Q.push(s);
while(!Q.empty()){
int top=Q.front();Q.pop();
for(int i=head[top];i;i=e[i].next){
int v=e[i].to;
if(dep[v]||!e[i].f) continue;
dep[v]=dep[top]+;Q.push(v);
}
}
return dep[t];
}
int cur[MAXN];
int dfs(int x,int flow,int t){
if(x==t) return flow;
int used=,tmp;
for(int i=head[x];i;i=e[i].next){
int v=e[i].to;
if(dep[v]!=dep[x]+) continue;
tmp=dfs(v,min(flow-used,e[i].f),t);
e[i].f-=tmp;e[i^].f+=tmp;used+=tmp;
if(used==flow) return flow;
if(e[i].f) cur[x]=i;
}
if(!used) dep[x]=-;
return used;
}
int dinic(int s,int t){
int ret=;
while(bfs(s,t)){
memcpy(cur,head,sizeof(head));
ret+=dfs(s,<<,t);
}
return ret;
} int main(){
n=rd();m=rd();num=rd();
int S=n*m+,T=n*m+;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
b[++p]=a[i][j]=rd();
}
}
int ans=<<;
sort(b+,b++p);
int l=,r=p;
while(l<=r){
int mid=l+r>>;
ecnt=;
memset(head,,sizeof(head));
for(int i=;i<=n;i++)add(S,i,),add(i,S,);
for(int i=;i<=m;i++)add(i+n,T,),add(T,i+n,);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(a[i][j]>b[mid]) continue;
add(i,j+n,);
add(j+n,i,);
}
}
int tmp=dinic(S,T);
if(tmp>=n-num+) r=mid-,ans=b[mid];
else l=mid+;
}
cout<<ans;
return ;
}