【二分图匹配入门专题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;
 } 
时间: 2024-11-05 21:49:38

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

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximu

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total len

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every stude

【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to