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;
     } 
  • 相关阅读:
    深入理解Java内存(图解堆栈)
    java堆栈区别
    Java之堆栈的区别
    【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片
    【微信小程序】转载:微信小程序实战篇-下拉刷新与加载更多
    【微信小程序】转载:微信小程序之购物车功能
    【微信小程序】loading标签使用,可自定义时长
    【微信小程序】日历插件,适用于酒店订房类小程序
    【微信小程序】微信小程序wx.previewImage预览图片
    【微信小程序】小程序和公众号 退款功能教程(含申请退款和退款回调,退款回调地址在商户后台配置或者代码自定义)
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7353238.html
Copyright © 2011-2022 走看看