POJ 2289--Jamie's Contact Groups【二分图多重匹配问题 &&二分查找最大值的最小化 && 最大流求解】

Jamie's Contact Groups
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 6902 Accepted: 2261

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 个组(0 ~ m - 1)。每个人只能选择一个组,现在给出每个人可以选择的组号,设拥有人数最多的小组有 sum个人,问sum的最小值。


解析:要求最大值的最小化,第一反应就是用二分查找。二分枚举人最多的小组拥有的人数mid。

首先构造网络流, 建立超级源点, 超级汇点。

源点到每个人建边,权值为1。

每个人到可选择的小组建边,权值为1。

每个小组到汇点建边,权值为mid。

每次跑一遍网络流,如果满流的话,人数最多的小组拥有 mid 人,这种情况是可能存在的,如果当前大值的最小值 < mid, 更新最大值的最小值。



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 2000
#define maxm 1000000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;

struct node {
    int u, v, cap, flow, next;
};
node edge[maxm];
node newedge[maxm];
int head[maxn], cnt;
int newhead[maxn], newcnt;
int cur[maxn];
int dist[maxn], vis[maxn];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    node E;
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0 ,head[v]};
    head[v] = cnt++;
}

void getmap(){
    char str[100];
    for(int i = 1; i <= n; ++i){
        add(0, i, 1);//源点到每个人建边,权值为1
        scanf("%s", str);
        int a;
        while(getchar() != '\n'){
            scanf("%d", &a);
            add(i, a + 1 + n, 1);//每个人到对应的组建边,权值为1
        }
    }
}

bool BFS(int st ,int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed)
                    return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0)
                break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st,ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

int main(){
    while(scanf("%d%d", &n, &m), n || m){
        init();
        getmap();
        memcpy(newhead, head, sizeof(head));
        memcpy(newedge, edge, sizeof(edge));
        newcnt = cnt;
        int l = 0, r = n, mid;
        int ans = n;
        while(r >= l){
            memcpy(head, newhead, sizeof(newhead));
            memcpy(edge, newedge, sizeof(newedge));
            cnt = newcnt;
            mid = (l + r) / 2;
            for(int i = 1 ;i <= m; ++i){
                add(i + n, n + m + 1, mid);//每个组向汇点建边
            }
            if(maxflow(0, n + m + 1) == n){
                ans = min(ans, mid);
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
    printf("%d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值