码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2289 最大流

时间:2017-05-25 23:35:08      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:iss   bool   eps   source   using   end   ble   style   ios   

Jamie‘s Contact Groups
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 7624   Accepted: 2562

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

Source

题意:
有n个联系人,m个分类,给出每个联系人可以属于的分类,问把所有的联系人归类后,联系人数量最多的一类最少可以有几个联系人
代码:
//二分最大值mid,源点连向n个联系人权值为1,联系人连向它可以属于的分类,m个分类与汇点建边权值为mid(该分类最多mid个人)
//看最大流是否等于n。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2009;
const int inf=0x7fffffff;
vector<int>v[maxn];
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>g[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void Init(int n){
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void Addedge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向弧
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool Bfs(){
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<(int)g[x].size();i++){
                Edge &e=edges[g[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int Dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int&i=cur[x];i<(int)g[x].size();i++){
            Edge &e=edges[g[x][i]];
            if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[g[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t){
        this->s=s;this->t=t;
        int flow=0;
        while(Bfs()){
            memset(cur,0,sizeof(cur));
            flow+=Dfs(s,inf);
        }
        return flow;
    }
}dc;

int n,m;
char ch[20];

bool solve(int mid){
    dc.Init(n+m+2);
    for(int i=1;i<=n;i++){
        dc.Addedge(0,i,1);
        for(int j=0;j<(int)v[i].size();j++)
            dc.Addedge(i,v[i][j]+n,1);
    }
    for(int i=n+1;i<=n+m;i++){
        dc.Addedge(i,n+m+1,mid);
    }
    return dc.Maxflow(0,n+m+1)==n;
}

int main()
{
    while(scanf("%d%d",&n,&m)&&(n+m)){
        for(int i=1;i<=n;i++){
            scanf("%s",ch);
            while(getchar()!=\n){
                int x;
                scanf("%d",&x);
                x++;
                v[i].push_back(x);
            }
        }
        int l=0,r=n,mid,ans=0;
        while(l<=r){
            mid=(l+r)/2;
            if(solve(mid)){
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++) v[i].clear();
    }
    return 0;
}

 

POJ 2289 最大流

标签:iss   bool   eps   source   using   end   ble   style   ios   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6906245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!