• HDU 1669 二分图多重匹配+二分


    Jamie's Contact Groups

    Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 747    Accepted Submission(s): 303


    Problem 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
     
    解析:有n个人,m个分组,每个人都可以分到指定的几个组合,问怎么分组使得m个组中的最大组的大小最小。
    显然是一对多的关系,然后我们二分 二分图多重匹配 的限制,找到可以使n个人都可以匹配的最小限制就好了。
     
    AC代码
    #include <bits/stdc++.h>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define all(a) (a).begin(), (a).end()
    #define fillchar(a, x) memset(a, x, sizeof(a))
    #define huan printf("
    ")
    #define debug(a,b) cout<<a<<" "<<b<<" "<<endl
    #define ffread(a) fastIO::read(a)
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int MAXN = 1000+10;
    const int MAXM = 500+10;
    int uN,vN;
    int g[MAXN][MAXM];
    int linker[MAXM][MAXN];
    bool used[MAXM];
    int num[MAXM],limit;
    bool dfs(int u)
    {
        for(int v = 0; v < vN; v++)
            if(g[u][v] && !used[v])
            {
                used[v] = true;
                if(linker[v][0] < limit)
                {
                    linker[v][++linker[v][0]] = u;
                    return true;
                }
                for(int i = 1; i <= linker[v][0]; i++)
                    if(dfs(linker[v][i]))
                    {
                        linker[v][i] = u;
                        return true;
                    }
            }
        return false;
    }
    int hungary()
    {
        int res = 0,flag=0;
        for(int i = 0; i < vN; i++)
            linker[i][0] = 0;
        for(int u = 0; u < uN; u++)
        {
            memset(used,false,sizeof(used));
            if(dfs(u))
                res++;
            else
            {
                flag=1;
                break;
            }
        }
        return flag;
    }
    int main()
    {
        while(scanf("%d%d",&uN,&vN)!=EOF&&uN&&vN)
        {
            fillchar(g,0);
            getchar();
            for(int i=0;i<uN;i++)
            {
                char s[2000];
                gets(s);
                int len=strlen(s);
                int num=0,jishu=0;
                for(int j=len-1;j>=0;j--)
                {
                    if(s[j]==' ')
                    {
                        g[i][num]=1;
                        num=jishu=0;
                    }
                    else if(s[j]>='0'&&s[j]<='9')
                    {
                        num+=(int)(s[j]-'0')*pow(10,jishu);
                        jishu++;
                    }
                    else
                        break;
                }
            }
            int l=0,r=uN;
            while(l<=r)
            {
                limit=(l+r)/2;
                //cout<<limit<<endl;
                if(!hungary())
                    r=limit-1;
                else
                    l=limit+1;
            }
            printf("%d
    ",r+1);
        }
    }
  • 相关阅读:
    [POJ3635]Full Tank? 题解
    洪水题解
    [HNOI2009]最小圈 题解
    Grazing on the Run 题解
    [BZOJ4237]稻草人 题解
    [POJ3783]Balls 题解
    [POI2005]Bank notes 题解
    字符串题解
    pyinstaller利用spec文件打包的使用模板
    Pycharm2020 永久激活
  • 原文地址:https://www.cnblogs.com/stranger-/p/9822440.html
Copyright © 2020-2023  润新知