lightoj 1184 Marriage Media [二分图匹配]

来源:互联网 发布:乐视mac 编辑:程序博客网 时间:2024/04/29 23:53

Description

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


题目大意:给你n个男的,m个女的 ; 男的和女的体重差不能超过15,年龄差不能超过5,婚姻状态必须相同,求满足个数;

AC代码:

#include <bits/stdc++.h>using namespace std ;int ma[500][500] , link[500] , vis[500] , n , m ;int age[500] , wei[500] , marry[500] ;int find( int x){    for(int i = 1 ; i <= m;i++)    {        if(!vis[i]&&ma[x][i]==1)        {            vis[i] = 1 ;            if(!link[i]||find(link[i]))            {                link[i] = x ;                return 1 ;            }        }    }    return 0 ;}int main(){    int t ,ans , count = 1;    cin>>t;    while(t--)    {        memset(ma,0,sizeof(ma));        memset(link,0,sizeof(link));        cin>>n>>m;        ans = 0 ;        for(int i = 1 ; i <=n+m;i++) cin>>wei[i]>>age[i]>>marry[i];        for(int i = 1 ; i<=n ; i++)        {            for(int j =1 ; j<=n+m;j++)            {                if(abs(wei[i]-wei[j])<=12)                if(abs(age[i]-age[j])<=5)                if(marry[i]==marry[j])                    ma[i][j-n] = 1 ;            }        }        for(int i = 1 ; i <=n ; i++)        {            memset(vis,0,sizeof(vis));            if(find(i)) ans++;        }        printf("Case %d: %d\n",count++,ans);    }    return 0 ;}


0 0