题目大意:给你N个男的,M个女的,要求你尽量的配对
两个人配对的话要满足以下条件:
1.异性
2.身高差不超12
3.年龄差不超5
4.婚姻状态相同
解题思路:二分图最大匹配,处理好边就好了
#include <cstdio>
#include <cstring>
#include <cstdlib>
const int N = 55;
const int M = 110;
int n, m, cas = 1;
int height[M], age[M], statu[M], left[N];
bool can[N][N];
bool vis[N];
void init() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n + m; i++)
scanf("%d%d%d", &height[i], &age[i], &statu[i]);
memset(can, 0, sizeof(can));
for (int i = 1; i <= n; i++) {
for (int j = n + 1; j <= n + m; j++) {
if (abs(height[i] - height[j]) > 12)
can[i][j - n] = 1;
if (abs(age[i] - age[j]) > 5)
can[i][j - n] = 1;
if (statu[i] + statu[j] == 1)
can[i][j - n] = 1;
}
}
}
bool dfs(int u) {
for (int i = 1; i <= m; i++) {
if (!can[u][i] && !vis[i]) {
vis[i] = true;
if (!left[i] || dfs(left[i])) {
left[i] = u;
return true;
}
}
}
return false;
}
void solve() {
int ans = 0;
memset(left, 0, sizeof(left));
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof (vis));
if (dfs(i)) ans++;
}
printf("Case %d: %d\n", cas++, ans);
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}