zoukankan      html  css  js  c++  java
  • 【二分图匹配入门专题1】H

    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
    ",t2,sum);
        }
        return 0;
     } 
  • 相关阅读:
    IO库 8.5
    IO库 8.4
    标准模板库——IO库
    IO库 8.3
    IO库 8.2
    IO库 8.1
    CF 599D Spongebob and Squares(数学)
    Django入门学习(一)
    hdu 5733
    uva 11210
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7353238.html
Copyright © 2011-2022 走看看