【POJ 2289】 Jamie's Contact Groups 【网络流+二分】

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个小组,要求每一个人只能属于一个小组,现在已经给出每个人可以归属的小组编号(从0到M-1)。设所有小组中人数的 最多的小组所拥有的人数为num,现在让你求最小的num.

分析:最小化最大值,明显要用二分,题目是人员分配的问题,所以我们可用网络流。
很常见的建图,这里就不画了。
代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)

const int MAXN =  2000+11;
const int MAXM = 600000+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Edge {
    int form,to,cap,flow,nexts;
}edge[MAXM];
int head[MAXN],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b,int c){
    Edge e={a,b,c,0,head[a]};
    edge[top]=e;head[a]=top++;

    Edge ee={b,a,0,0,head[b]};
    edge[top]=ee;head[b]=top++;
}
int n,m;
int S,T;
int mp[MAXN][MAXN];
void getmap(int mid){
    S=0,T=n+m+1;
     for(int i=1;i<=max(n,m);i++){
        if(i<=n) addedge(S,i,1);
        addedge(i+n,T,mid);
     }
     for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(mp[i][j]) 
                addedge(i,j+n,1);
         }
     }
}

int vis[MAXN],dis[MAXN];
int cur[MAXN];
bool bfs(int st,int ed){
    queue<int>Q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    Q.push(st);vis[st]=1;dis[st]=1;
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=head[now];i!=-1;i=edge[i].nexts){
            Edge e=edge[i];
            if(!vis[e.to]&&e.cap-e.flow>0){
                vis[e.to]=1;
                dis[e.to]=dis[now]+1;
                if(e.to==ed) return 1;
                Q.push(e.to);
            }
        }
    }
    return 0;
}
int dfs(int now,int a,int ed){
    if(a==0||now==ed) return a;
    int flow=0,f;
    for(int &i=cur[now];i!=-1;i=edge[i].nexts){
        Edge &e=edge[i];
        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap-e.flow,a),ed))>0){
            e.flow+=f;
            flow+=f;
            edge[i^1].flow-=f;
            a-=f;
            if(a==0) break;
        } 
    }
    return flow;
}
int max_flow(int st ,int ed){
    int flow=0;
    while(bfs(st,ed)){
        memcpy(cur,head,sizeof(head));
        flow+=dfs(st,inf,ed);
    }
    return flow;
}
void solve(){
    int l=1,r=inf;
    int ans;
    while(l<=r){
        init();
        int mid=(l+r)>>1;
        getmap(mid);
        if(max_flow(S,T)>=n) {
            ans=mid; r=mid-1;
        }else l=mid+1;
        //printf("%d\n",ans);
    }
    printf("%d\n",ans);
}
int main(){  
//  fread();
//  fwrite();
    while(scanf("%d%d",&n,&m)&&(n||m)){
        int a;
        memset(mp,0,sizeof(mp));
        char s[50];
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            char ch=' ';
            while(ch!='\n'){
                scanf("%d%c",&a,&ch);a++;
                mp[i][a]=1;
            //  printf("%d %d\n",i,a);
             }
         }
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值