用的网络瘤写的二分图(但是只能AC 5,7,10三个点
#include<bits/stdc++.h>
using namespace std;
const int N=5e3+10;
const int inf=0x3f3f3f3f;
int h[N],e[N<<4],ne[N<<4],w[N<<4];
int idx;
void add(int a,int b,int c)
{
e[idx]=b;
ne[idx]=h[a];
h[a]=idx;
w[idx++]=c;
e[idx]=a;
ne[idx]=h[b];
h[b]=idx++;
}
int now[N],d[N];
int s,t;
bool bfs()
{
queue<int> q;
for(int i=s;i<=t;i++) d[i]=0;
q.push(s);
now[s]=h[s];
d[s]=1;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=h[x];i!=-1;i=ne[i])
if(!d[e[i]]&&w[i])
{
d[e[i]]=d[x]+1;
q.push(e[i]);
now[e[i]]=h[e[i]];
if(e[i]==t) return 1;
}
}
return 0;
}
int maxflow;
int dinic(int x,int flow)
{
if(x==t) return flow;
int rest=flow,k,i;
for(int i=now[x];i!=-1&&rest;i=ne[i])
if(d[e[i]]==d[x]+1&&w[i])
{
k=dinic(e[i],min(rest,w[i]));
if(!k) d[e[i]]=0;
w[i]-=k;
w[i^1]+=k;
rest-=k;
}
now[x]=i;
return flow-rest;
}
int T;
int sa,sb,sc;
int mp[4][N],top;
int main() {
cin>>T;
while(T--)
{
int ans=inf;
top=0;
cin>>sa>>sb>>sc;
for(int i=1;i<=sa;i++)
for(int j=1;j<=sb;j++)
for(int k=1;k<=sc;k++)
{
int x;
cin>>x;
if(x)
{
top++;
mp[1][top]=i;
mp[2][top]=j;
mp[3][top]=k;
}
}
if(sa>sb) swap(sa,sb),swap(mp[1],mp[2]);
if(sa>sc) swap(sa,sc),swap(mp[1],mp[3]);
for(int i=0;i<(1<<sa);i++)
{
int x=i;
maxflow=0;
t=sb+sc+1;
for(int j=s;j<=t;j++) h[j]=-1;
idx=0;
while(x)
{
if(x&1) maxflow++;
x>>=1;
}
for(int j=1;j<=top;j++)
{
int x=mp[1][j],y=mp[2][j],z=mp[3][j];
if((1<<x-1)&i) continue;
add(y,sb+z,1);
}
for(int j=1;j<=sb;j++) add(s,j,1);
for(int j=1;j<=sc;j++) add(sb+j,t,1);
int flow=0;
while(bfs()) maxflow+=dinic(s,inf);
ans=min(maxflow,ans);
}
cout<<ans<<endl;
}
}