light oj 1184 Marriage Media

题目:

You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria.

  1. No man will marry a woman if their height gap is greater than 12 inches.
  2. No woman will marry a man if their age gap is greater than 5 years.
  3. A couple can be formed if either both are not divorced or both are divorced.
  4. Of course, a man can marry a single woman and vice versa.

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

Input

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

Each case contains two integer m, n (1 ≤ m, n ≤ 50). Each of the next m lines will contain the information for a man, and each of the next n lines will contain the information for a woman. An information will contain three integers denoting the height in inches, age in years and 1 or 0 depending on 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

题意描述:

很有意思的题目,给你m个男士和n个女士的身高、年龄、和婚姻状况,问你最多能凑成多少对。

解题思路:

二分最大匹配问题,使用匈牙利算法,改变一下判断条件即可。

AC代码:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 struct P
 5 {
 6     int h,a,f,p;
 7 };
 8 struct P man[55],wom[55];
 9 int m,n,book[55];
10 int maxmatch();
11 int path(int u);
12 int main()
13 {
14     int T,c=1,i;
15     scanf("%d",&T);
16     while(T--)
17     {
18         scanf("%d%d",&m,&n);
19         for(i=1;i<=m;i++)
20             scanf("%d%d%d",&man[i].h,&man[i].a,&man[i].f);
21         for(i=1;i<=n;i++)
22             scanf("%d%d%d",&wom[i].h,&wom[i].a,&wom[i].f);
23             
24         printf("Case %d: %d\n",c++,maxmatch());
25         /*for(i=1;i<=m;i++)
26             printf("%d号和%d号般配\n",i,man[i].p);*/
27     }
28     return 0;
29 } 
30 
31 int maxmatch()
32 {
33     int i,res=0;
34     for(i=1;i<=n;i++)
35         wom[i].p=0;
36     for(i=1;i<=m;i++)
37         man[i].p=0;
38     for(i=1;i<=m;i++)
39     {
40         if(!man[i].p)
41         {
42             memset(book,0,sizeof(book));
43             res += path(i);
44         }
45     }
46     return res;
47 }
48 int path(int u)
49 {
50     int i;
51     for(i=1;i<=n;i++)
52     {
53         if(!book[i] && fabs(man[u].h-wom[i].h)<=12
54         && fabs(man[u].a-wom[i].a)<=5 && man[u].f==wom[i].f)
55         {
56             book[i]=1;
57             if(!wom[i].p || path(wom[i].p))
58             {
59                 man[u].p=i;
60                 wom[i].p=u;
61                 //printf("男%d号和女%d号般配\n",u,man[u].p);
62                 return 1;
63             }
64         }
65     }
66     return 0;
67 }

 

posted @ 2017-08-15 09:16  Reqaw  阅读(165)  评论(0编辑  收藏  举报