【二分图匹配入门专题1】H - Marriage Media light oj 1184【二分图最大匹配】

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

题意:男的和女的要结婚得满足四个条件,1,男的身高不能高于女生12英尺;2,男的年龄不能大于女生5岁;3,男的和女的婚姻状况相同才能结婚;4,男的可以找单身女的结婚,反之亦然(废话)。要求输出最大配对数。

思路,就是一个二分图最大匹配模板题,只不过多了判断条件,对男的和女的分别编号,满足条件的进行配对,输出,完。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 1100
int book[N],e[N][N],match[N];
int n,m;

struct criteria
{
    int height;
    int age;
    int condition;
};
criteria man[N],woman[N];

int dfs(int u)
{
    int j;
    for(j = 1; j <= m; j ++)
    {
        if(!book[j]&&e[u][j])
        {
            book[j] = 1;
            if(!match[j]||dfs(match[j]))
            {
                match[j] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int t,i,j,sum;
    scanf("%d",&t);
    int t2 = 0;
    while ( t--)
    {
        t2 ++;
        scanf("%d%d",&n,&m);
        for(i = 1; i <= n; i ++)
            scanf("%d%d%d",&man[i].height ,&man[i].age ,&man[i].condition );
        for(i = 1; i <= m; i ++)
            scanf("%d%d%d",&woman[i].height ,&woman[i].age ,&woman[i].condition );
        memset(e,0,sizeof(e));
        memset(match,0,sizeof(match));
        for(i = 1; i <= n; i ++)
        {
            for(j = 1; j <= m; j ++)
            {
                if(fabs(man[i].height-woman[j].height )<=12)
                    if(fabs(man[i].age -woman[j].age )<=5)
                        if(man[i].condition==woman[j].condition)
                        {    
                            e[i][j] = 1;
                        }
            }
        }
        sum = 0;
        for(i = 1; i <= n; i ++)
        {
            memset(book,0,sizeof(book));
            if(dfs(i))
                sum ++;
        }
        printf("Case %d: %d\n",t2,sum);
    }
    return 0;
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值