码迷,mamicode.com
首页 > 其他好文 > 详细

Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

时间:2016-05-13 01:48:56      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

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 == '\n')
                    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\n", ans);
    }
    return 0;
}


Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

标签:

原文地址:http://blog.csdn.net/u014665013/article/details/51344918

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!