zoukankan      html  css  js  c++  java
  • Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

    Jamie's Contact Groups
    Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only one of those groups and the size of the largest group is minimized.

    Input

    There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the last test case, there is a single line `0 0' that terminates the input.

    Output

    For each test case, output a line containing a single integer, the size of the largest contact group.

    Sample Input

    3 2
    John 0 1
    Rose 1
    Mary 1
    5 4
    ACM 1 2 3
    ICPC 0 1
    Asian 0 2 3
    Regional 1 2
    ShangHai 0 2
    0 0
    

    Sample Output

    2
    2

    我的第一道二分图多重匹配题:

    转一个不错的Blog:http://www.cnblogs.com/zhengguiping--9876/p/4728358.html

    Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是多

    少?

     

    一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合x中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将

    xi,yi匹配。否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查是否能找到一条增广路径,如果能,则让出位置,让xi与yi匹配。

    二分求出limit,知道找到可以构成多重匹配的最小限制limit,在main函数中二分搜索。


    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define N 1010
    int vis[N], maps[N][N], ans, n, m;
    
    struct node
    {
        int cnt;///和yi相匹配的个数;
        int k[N];///和yi相匹配的x的集合;
    }Linky[N];
    
    bool Find(int u, int limit)
    {
        for(int i=1; i<=m; i++)
        {
            if(!vis[i] && maps[u][i])
            {
                vis[i] = 1;
                if(Linky[i].cnt < limit)
                {
                    Linky[i].k[ Linky[i].cnt++ ] = u;
                    return true;
                }
                for(int j=0; j<Linky[i].cnt; j++)
                {
                    if(Find( Linky[i].k[j], limit ))
                    {
                        Linky[i].k[j] = u;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    bool hungary(int limit)///匈牙利算法;
    {
        memset(Linky, 0, sizeof(Linky));
        for(int i=1; i<=n; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(!Find(i, limit))///当前的limit让i没有匹配,所以不能用limit;
                return false;
        }
        return true;
    }
    
    int main()
    {
        int x;
        char s[20], ch;
        while(scanf("%d %d", &n, &m), m+n)
        {
            memset(maps, 0, sizeof(maps));
            for(int i=1; i<=n; i++)
            {
                scanf("%s", s);
                while(1)
                {
                    scanf("%d%c", &x, &ch);
                    maps[i][x+1] = 1;
                    if(ch == '
    ')
                        break;
                }
            }
            int L = 1, R = n;
            ans = n;
            while(L <= R)
            {
                int mid = (L+R)/2;
                if(hungary(mid))///如果当前mid满足题意;
                {
                    R = mid-1;
                    ans = mid;
                }
                else
                    L = mid+1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }


  • 相关阅读:
    RabbitMQ详解(二)——
    Redis6详解(二)——常用命令
    MybatisPlus(二)——
    数据结构与算法(五)——树
    数据结构与算法(四)——队列
    数据结构与算法(三)——栈
    MybatisPlus(一)——
    Docker详解(一)——
    kafka详解(一)——
    FIle类操作
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717976.html
Copyright © 2011-2022 走看看