Loj全A,Luogu全T?
查看原帖
Loj全A,Luogu全T?
56825
oisdoaiu楼主2020/5/7 16:27

RT

#include<bits/stdc++.h>
using namespace std;

template<typename T>
inline void Read(T &n){
	char ch; bool flag=0;
	while(!isdigit(ch=getchar()))if(ch=='-')flag=1;
	for(n=ch^48;isdigit(ch=getchar());n=(n<<1)+(n<<3)+(ch^48));
	if(flag)n=-n;
}

const int MAXN = 5005;

int a[MAXN], n, m, p, Need[MAXN];

inline int ID(int x, int y){return (x-1)*m+y;}
inline int ID(int x, int y, int z){return (z-1)*n*m+(x-1)*m+y;}

int Match[MAXN], vis[MAXN];
int vis_time;

bool matching(int x){
	for(register int i=1; i<=m; i++)
		if(a[ID(x,i)] and vis[i]!=vis_time){
			vis[i] = vis_time;
			if(Match[i]==false or matching(Match[i])==true){
				Match[i] = x;
				return true;
			}
		}
	return false;
}

inline int Solve(){
	fill(vis+1,vis+m+1,0);
	fill(Match+1,Match+m+1,0);
	vis_time=1;
	int ans=0;
	for(register int i=1; i<=n; i++)
		if((vis_time=i) and matching(i))
			ans++;
	return ans;
}

int dfs(int h){
	if(h==p+1) return Solve();
	int res=INT_MAX;
	res = min(res,dfs(h+1)+1);
	for(register int i=1; i<=n; i++)for(register int j=1; j<=m; j++)a[ID(i,j)]+=Need[ID(i,j,h)];
	res = min(res,dfs(h+1));
	for(register int i=1; i<=n; i++)for(register int j=1; j<=m; j++)a[ID(i,j)]-=Need[ID(i,j,h)];
	return res;
}

int main(){
	int T; Read(T);
	while(T--){
		Read(n); Read(m); Read(p);
		if(p>n) swap(n,p);
		if(p>m) swap(m,p);
		for(register int k=1; k<=p; k++)
			for(register int i=1; i<=n; i++)
				for(register int j=1; j<=m; j++)
					Read(Need[ID(i,j,k)]);
		printf("%d\n",dfs(1));
	}
	return 0;
}
2020/5/7 16:27
加载中...