8.15 H

来源:互联网 发布:网络媒体传播力 知乎 编辑:程序博客网 时间:2024/06/06 00:32

H - Marriage Media

 You run a marriagemedia. You take some profiles for men and women, and your task is to arrange asmuch marriages as you can. But after reading their bio-data you have found thefollowing criteria.

1.      Noman will marry a woman if their height gap is greater than 12 inches.

2.      Nowoman will marry a man if their age gap is greater than 5 years.

3.      Acouple can be formed if either both are not divorced or both are divorced.

4.      Ofcourse, a man can marry a single woman and vice versa.

Now you are giventhe bio-data of some men and women, you have to arrange the maximum numberof marriages considering the given criteria.

Input

Input starts withan integer T (≤ 200), denoting the number of test cases.

Each case containstwo integer m, n (1 ≤ m, n ≤ 50). Each of the next m lineswill contain the information for a man, and each of the next n lineswill contain the information for a woman. An information will contain threeintegers denoting the height in inches, age in years and 1 or 0 dependingon they are divorced or not respectively. Assume that Height will be between 50 and 80,age will be between 20 and 50.

Output

For each case,print the case number and the maximum number of marriages you can arrange.

Sample Input

2

2 2

70 30 0

60 20 0

71 25 0

71 35 0

1 1

70 30 1

70 30 0

Sample Output

Case 1: 2

Case 2: 0

 

题意:一个婚姻介绍所,给出一些男女的身高,年龄,是否已婚情况,根据以下条件进行配对:1.如果身高差距大于12英寸,没有人会娶一个女人。2.如果年龄差距大于5岁,没有女人会嫁给一个男人。2.如果双方都不离婚,或者双方都离婚了,可以形成一对夫妇。4.当然,男人可以娶一个单身女人,反之亦然。输出最大匹配数。

思路:先根据题中所给条件判断每一位男士的能够与其配对的女士,构建图,然后用匈牙利算法计算。


#include<stdio.h>#include<string.h>int a[100][100],b[100],match[100],s1[100][10],s2[100][10];int m,n;int ads(int x){if(x<0)x=-x;return x;}int dfs(int s)//匈牙利算法{int i;for(i=1;i<=m;i++){if(b[i]==0&&a[s][i]==1){b[i]=1;if(match[i]==-1||dfs(match[i])){match[i]=s;return 1;}}}return 0;}int main(){int i,j,k,sum,t,z=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);memset(a,0,sizeof(a));memset(match,-1,sizeof(match));for(i=1;i<=n;i++){scanf("%d%d%d",&s1[i][0],&s1[i][1],&s1[i][2]);}for(i=1;i<=m;i++){scanf("%d%d%d",&s2[i][0],&s2[i][1],&s2[i][2]);}for(i=1;i<=n;i++){for(j=1;j<=m;j++){
if(s1[i][2]==s2[j][2]&&ads(s1[i][0]-s2[j][0])<=12&&ads(s1[i][1]-s2[j][1])<=5)//根据题意构建图{a[i][j]=1;}}}sum=0;for(i=1;i<=n;i++){memset(b,0,sizeof(b));if(dfs(i))sum++;}printf("Case %d: %d\n",z++,sum);}return 0;}



原创粉丝点击