首页 > 代码库 > UValive3268 Jamie's Contact Groups(二分+最大流)

UValive3268 Jamie's Contact Groups(二分+最大流)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1269

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

这个题的输入略坑。。。做这题大部分时间都花在了处理输入上。。。最后直接用的字符输入,然后碰到数字字符再转换成数字的方法。。。

建图思路是建源点与汇点,将人与源点相连,权值为1,将人与组相连,权值也为1(其实只要大于等于1就行)。然后再将组与汇点相连,权值是二分到的最大值。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;
const int INF=1e9;
int head[2000], s, t, nv, n, cnt;
int num[2000], d[2000], pre[2000], cur[2000], q[2000], pipei[1001][1001];
struct node
{
    int u, v, cap, next;
}edge[1000000];
void add(int u, int v, int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void bfs()
{
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));
    int f1=0, f2=0, i;
    q[f1++]=t;
    d[t]=0;
    num[0]=1;
    while(f1>=f2)
    {
        int u=q[f2++];
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(d[v]==-1)
            {
                d[v]=d[u]+1;
                num[d[v]]++;
                q[f1++]=v;
            }
        }
    }
}
int isap()
{
    memcpy(cur,head,sizeof(cur));
    int flow=0, u=pre[s]=s, i;
    bfs();
    while(d[s]<nv)
    {
        if(u==t)
        {
            int f=INF, pos;
            for(i=s;i!=t;i=edge[cur[i]].v)
            {
                if(f>edge[cur[i]].cap)
                {
                    f=edge[cur[i]].cap;
                    pos=i;
                }
            }
            for(i=s;i!=t;i=edge[cur[i]].v)
            {
                edge[cur[i]].cap-=f;
                edge[cur[i]^1].cap+=f;
            }
            flow+=f;
            if(flow>=n)
                return flow;
            u=pos;
        }
        for(i=cur[u];i!=-1;i=edge[i].next)
        {
            if(d[edge[i].v]+1==d[u]&&edge[i].cap)
            {
                break;
            }
        }
        if(i!=-1)
        {
            cur[u]=i;
            pre[edge[i].v]=u;
            u=edge[i].v;
        }
        else
        {
            if(--num[d[u]]==0) break;
            int mind=nv;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(mind>d[edge[i].v]&&edge[i].cap)
                {
                    mind=d[edge[i].v];
                    cur[u]=i;
                }
            }
            d[u]=mind+1;
            num[d[u]]++;
            u=pre[u];
        }
    }
    return flow;
}
int main()
{
    int m, i, j, x, z;
    char c, name[20];
    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
    {
        memset(pipei,0,sizeof(pipei));
        for(i=1;i<=n;i++)
        {
            scanf("%s",name);
            c=getchar();
            while(c!='\n')
            {
                x=0;
                z=0;
                c=getchar();
                while(c!=' '&&c!='\n')
                {
                    x=x*10+c-'0';
                    z=1;
                    c=getchar();
                }
                if(z)
                pipei[i][x+1]=1;
            }
        }
        int high=n, low=1, mid, y, ans;
        while(low<=high)
        {
            mid=(high+low)/2;
            s=0;
            t=n+m+1;
            nv=t+1;
            cnt=0;
            memset(head,-1,sizeof(head));
            for(i=1;i<=n;i++)
            {
                add(s,i,1);
                for(j=1;j<=m;j++)
                {
                    if(pipei[i][j])
                    {
                        add(i,j+n,1);
                    }
                }
            }
            for(i=1;i<=m;i++)
            {
                add(i+n,t,mid);
            }
            y=isap();
            //printf("%d\n",y);
            if(y>=n)
            {
                ans=mid;
                high=mid-1;
            }
            else
            {
                low=mid+1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}