题面

题解

当只有二维时,就是一个二分图匹配的板子题

三维的时候就很好做了,暴力枚举一维的情况,因为\(\min(x,y,z) = \sqrt{5000} < 18\),于是时间复杂度有保证

代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x)) inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
} const int maxn(5010);
struct edge { int next, to; } e[maxn];
int head[maxn], e_num, a, b, c, Min, pos[4][maxn], vis[maxn], match[maxn], clean[maxn], ans, cnt, T; inline void add_edge(int from, int to) { e[++e_num] = (edge) {head[from], to}; head[from] = e_num; }
bool dfs(int x)
{
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to]) continue; vis[to] = true;
if(!match[to] || dfs(match[to])) return match[to] = x, true;
}
return false;
} inline void Doit(int x)
{
using std::fill; e_num = 0;
fill(head + 1, head + b + 1, 0);
fill(match + 1, match + c + 1, 0);
fill(clean + 1, clean + a + 1, 1);
int res = 0;
for(RG int i = 0; i < a; i++)
if(x & (1 << i)) clean[i + 1] = 0, ++res;
for(RG int i = 1; i <= cnt; i++)
if(clean[pos[1][i]]) add_edge(pos[2][i], pos[3][i]);
for(RG int i = 1; i <= b; i++)
{
fill(vis + 1, vis + c + 1, 0);
if(dfs(i)) ++res;
}
ans = std::min(ans, res);
} int main()
{
T = read();
while(T--)
{
cnt = 0; ans = 0x3f3f3f3f;
a = read(); b = read(); c = read(); Min = std::min(std::min(a, b), c);
for(RG int i = 1, x; i <= a; i++)
for(RG int j = 1; j <= b; j++)
for(RG int k = 1; k <= c; k++)
if((x = read())) ++cnt, pos[1][cnt] = i, pos[2][cnt] = j, pos[3][cnt] = k;
using std::swap;
if(Min == b) swap(a, b), swap(pos[1], pos[2]);
if(Min == c) swap(a, c), swap(pos[1], pos[3]);
for(RG int i = 0; i < (1 << a); i++) Doit(i);
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. 判断字符串的首字母 ---------startsWith
  2. BootStrap2学习日记21---消息提示
  3. fawef
  4. JavaScript高级程序设计9.pdf
  5. Collection Views and Building Custom Layouts-备
  6. drawRect &amp; 内存 -&gt; 深究
  7. 【一天一道LeetCode】#7. Reverse Integer
  8. Mac 在terminal 上用命令打开sublime
  9. Canvas &amp; SVG
  10. ps: 图层样式;
  11. linux unknown host 问题【转】
  12. Codeforces 822E Liar dp + SA (看题解)
  13. C++ 预处理器
  14. individual project1 12061183
  15. Python进行URL解码
  16. 零基础Python爬虫实现(百度贴吧)
  17. mybatis之sql执行有数据但返回结果为null
  18. 【vue】父子组件间通信----传值
  19. 20155306 白皎 《网络攻防》 Exp2 后门原理与实践
  20. 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)

热门文章

  1. EF的连表查询Lambda表达式和linq语句
  2. unbind() 移除事件内处理方法
  3. [沫沫金]JavaWeb企业信息系统,增加操作记录、数据库记录
  4. CSS背景图片垂直居中center不起效果完美解决
  5. EntityFramework Code First便捷工具——数据迁移
  6. [EffectiveC++]item44:将与参数无关的代码抽离templates
  7. WCF自寄宿实现Https绑定
  8. 智能指针shared_ptr新特性shared_from_this及weak_ptr
  9. Python time.md
  10. C# 数字证书 RSA加密解密 加签验签