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

來源:互聯網
上載者:User

標籤:c語言   演算法   編程   網路流   uva   

題目地址: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 2John 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;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.