题目连接:3140:[Hnoi2013]消毒
首先如果只是两维的平面我们直接二分图覆盖集
那么三维的话枚举在x上枚举哪些层直接消去
剩下的同二维做法
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5111;
int n,m,tot=0,h[maxn],cnt=0,a,b,c;
struct edge{int to,next;}G[maxn];
struct point{
int x,y,z;
point(){}
point(int _x,int _y,int _z){
x=_x,y=_y,z=_z;
if (b<=a&&b<=c) swap(x,y);
else if (c<=a&&c<=b) swap(x,z);
}
}p[maxn];
int tim=0,vis[maxn],girl[maxn],ans;
bool mark[maxn];
void add(int x,int y){
G[++tot].to=y;G[tot].next=h[x];h[x]=tot;
}
bool find(int x){
for (int i=h[x];i;i=G[i].next){
int v=G[i].to;
if (vis[v]==tim) continue;
vis[v]=tim;
if (!girl[v]||find(girl[v])){
girl[v]=x; return 1;
}
}return 0;
}
void solve(int now){
for (int i=1;i<=max(b,c);++i) h[i]=0; tot=0;
for (int i=1;i<=max(b,c);++i) girl[i]=0;
for (int i=1;i<=cnt;++i)
if (!mark[p[i].x]) add(p[i].y,p[i].z);
for (int i=1;i<=b;++i){
++tim; now+=find(i);
if (now>=ans) return;
}
ans=now;
}
void work(){
ans=a;
for (int i=1;i<(1<<a);++i){
for (int j=1;j<=18;++j) mark[j]=0;
int sum=0;
for (int j=1;j<=a;++j) if (i&(1<<(j-1))) mark[j]=1,sum++;
if (sum>=ans) continue;
solve(sum);
}
}
int main(){
int T; scanf("%d",&T);
while (T--){
scanf("%d%d%d",&a,&b,&c);
cnt=0;
for (int i=1;i<=a;++i)
for (int j=1;j<=b;++j)
for (int k=1;k<=c;++k){
int x; scanf("%d",&x);
if (x) p[++cnt]=(point){i,j,k};
}
if (b<=a&&b<=c) swap(a,b);
else if (c<=a&&c<=b) swap(a,c);
work();
printf("%d\n",ans);
}
}