zoukankan      html  css  js  c++  java
  • 1345

    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
    

    第一道独立完成的最大流题,和la3231 : http://www.cnblogs.com/ramanujan/p/3329486.html 建模类似:

    1.可以先建成二分图:X中集合点为所有人,Y中节点为所有分组,每个人向可行分组连一条容量为1的弧,

    2.每个分组向虚拟汇点连一条容量为当前二分值大小的弧,从源点向每个人连一条容量为1的弧

    接下来二分+模板即可。

    记下自己犯的错:

    1.在isap函数中搞混了flow和ef值WA了N次,

    2.而且粗心把vis[s]初始化了true。。。

    3.一个问题:bfs中s点不可达怎么办(d[s])?

    ISAP:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define INF 10000000
    #define MAXN 1555
    #define MAXM 4444444
    
    struct edge{
        int u, v, cap, flow, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m;
    
    void add(int u, int v, int cap){
        e[cc]=(edge){u, v, cap, 0, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, 0, 0, h[v]};
        h[v]=cc++;
    }
    
    int d[MAXN];
    bool vis[MAXN];
    void bfs(int s, int t){                            //s不可达?
        memset(vis, 0, sizeof(vis));
        d[t]=0;     vis[t]=true;
        queue<int> q;       q.push(t);
        while(!q.empty()){
            int u=q.front();    q.pop();
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                i^=1;
                int v=e[i].u, cap=e[i].cap, flow=e[i].flow;
                if(!vis[v] && cap>flow){
                    vis[v]=true;
                    d[v]=d[u]+1;
                    q.push(v);
                }
                i^=1;
            }
        }
    }
    
    int cur[MAXN], p[MAXN];
    
    int Augment(int s, int t){
        int a=INF;
        for(int u=t; u!=s; u=e[p[u]].u)
            a=MIN(a, e[p[u]].cap-e[p[u]].flow);
        for(int u=t; u!=s; u=e[p[u]].u){
            e[p[u]].flow+=a;
            e[p[u]^1].flow-=a;
        }
        return a;
    }
    
    int cnt[MAXN];
    int isap(int s, int t, int k){
        int flow=0, i;
        bfs(s, t);
    //    memset(d, 0, sizeof(d));
        memset(cnt, 0, sizeof(cnt));
        for(i=0; i<k; i++) cur[i]=h[i];
        for(i=0; i<k; i++) cnt[d[i]]++;
        for(int u=s; d[s]<k; ){
            if(u==t){
                flow+=Augment(s, t);
                u=s;
            }
            bool ok=false;
            for(i=cur[u]; i!=-1; i=e[i].nxt){
                int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                if(d[v]+1==d[u] && cap>ef){
                    cur[u]=i;
                    ok=true;
                    p[v]=i;
                    u=v;
                    break;
                }
            }
            if(!ok){
                int tmp=k-1;
                for(i=h[u]; i!=-1; i=e[i].nxt){
                    int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                    if(cap>ef) tmp=MIN(tmp, d[v]);                      //--------------
                }
                if(--cnt[d[u]]==0) break;
                cnt[d[u]=tmp+1]++;
                cur[u]=h[u];
                if(u!=s) u=e[p[u]].u;
            }
        }
        return flow;
    }
    
    bool test(int x, const int tc){
        for(int i=tc; i<cc; i+=2) e[i].cap=x;
        for(int i=0; i<cc; i++) e[i].flow=0;
    
        return isap(0, n+m+1, n+m+2)==n;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            while((getchar())!='
    ');
            memset(h, -1, sizeof(h));       cc=0;
    
            for(int u=1; u<=n; u++){
                char name[19], ch;
                int v=0;
                bool f=false;
                scanf(" %s", name);
                while((ch=getchar())!='
    '){
                    if(ch==' ' && f){
                        add(u, v+1+n, 1);
                        v=0;     f=false;
                    }
                    else if(ch!=' '){
                        v = v * 10 + ch - '0';
                        f=true;
                    }
                }
                if(f) add(u, v+1+n, 1);
            }
            for(int u=1; u<=n; u++) add(0, u, 1);
    
            const int tc=cc;
            for(int u=n+1; u<=m+n; u++) add(u, n+m+1, n);
    
            int l=1, r=n, mid, ans=0;
            while(l<=r){
                mid=(l+r)>>1;
                if(test(mid, tc)) { ans=mid; r=mid-1; }
                else l=mid+1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

    Dinic版本:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define INF 10000000
    #define MAXN 1555
    #define MAXM 4444444
    
    struct edge{
        int u, v, cap, flow, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m;
    
    void add(int u, int v, int cap){
        e[cc]=(edge){u, v, cap, 0, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, 0, 0, h[v]};
        h[v]=cc++;
    }
    
    int d[MAXN];
    bool vis[MAXN];
    void rbfs(int s, int t){
        memset(vis, 0, sizeof(vis));
        d[t]=0;     vis[s]=true;
        queue<int> q;       q.push(t);
        while(!q.empty()){
            int u=q.front();    q.pop();
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                i^=1;
                int v=e[i].u, cap=e[i].cap, flow=e[i].flow;
                if(!vis[v] && cap>flow){
                    vis[v]=true;
                    d[v]=d[u]+1;
                    q.push(v);
                }
                i^=1;
            }
        }
    }
    
    int cur[MAXN], p[MAXN];
    
    int Augment(int s, int t){
        int a=INF;
        for(int u=t; u!=s; u=e[p[u]].u)
            a=MIN(a, e[p[u]].cap-e[p[u]].flow);
        for(int u=t; u!=s; u=e[p[u]].u){
            e[p[u]].flow+=a;
            e[p[u]^1].flow-=a;
        }
        return a;
    }
    
    int cnt[MAXN];
    int isap(int s, int t, int k){
        int flow=0, i;
        rbfs(s, t);
    //    memset(d, 0, sizeof(d));
        memset(cnt, 0, sizeof(cnt));
        for(i=0; i<k; i++) cur[i]=h[i];
        for(i=0; i<k; i++) cnt[d[i]]++;
        for(int u=s; d[s]<k; ){
            if(u==t){
                flow+=Augment(s, t);
                u=s;
            }
            bool ok=false;
            for(i=cur[u]; i!=-1; i=e[i].nxt){
                int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                if(d[v]+1==d[u] && cap>ef){
                    cur[u]=i;
                    ok=true;
                    p[v]=i;
                    u=v;
                    break;
                }
            }
            if(!ok){
                int tmp=k-1;
                for(i=h[u]; i!=-1; i=e[i].nxt){
                    int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                    if(cap>flow) tmp=MIN(tmp, d[v]);
                }
                if(--cnt[d[u]]==0) break;
                cnt[d[u]=tmp+1]++;
                cur[u]=h[u];
                if(u!=s) u=e[p[u]].u;
            }
        }
        return flow;
    }
    
    bool bfs(int s, int t){
        int i, u;
        memset(vis, 0, sizeof(vis));
    //    for(i=0; i<k; i++) d[i]=INF;
        queue<int> q;   q.push(s);
        vis[s]=true;        d[s]=0;
    //    cout<<'a'<<endl;
        while(!q.empty()){
            u=q.front();    q.pop();
    //        cout<<u<<endl;
            for(i=h[u]; i!=-1; i=e[i].nxt){
                int cap=e[i].cap, flow=e[i].flow, v=e[i].v;
    //            cout<<done[v]<<' '<<cap<<' '<<flow<<endl;
                if(!vis[v] && cap>flow){
    //                cout<<v<<' ';
                    d[v]=d[u]+1;
                    vis[v]=true;
                    q.push(v);
                }
            }
    //        cout<<endl;
        }
        return vis[t];
    }
    
    int dfs(int u, int a, int t){
        if(u==t || a==0) return a;
        int flow=0, f;
        for(int &i=cur[u]; i!=-1; i=e[i].nxt){                  //multi path augmenting, 不用优化cur会TLE
            int v=e[i].v;
            if(d[u]+1 == d[v] && (f=dfs(v, MIN(a, e[i].cap-e[i].flow), t))>0){
                e[i].flow+=f;
                e[i^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }
    
    //int tc;
    bool Dinic(int x, const int tc){
        for(int i=tc; i<cc; i+=2) e[i].cap=x;
        for(int i=0; i<cc; i++) e[i].flow=0;
    
        int flow=0;
        while(bfs(0, n+m+1)){
            for(int i=0; i<n+m+2; i++) cur[i]=h[i];
            flow+=dfs(0, INF, n+m+1);
            //cout<<flow<<endl;
        }
        return flow==n;
    }
    
    bool test(int x, const int tc){
        for(int i=tc; i<cc; i+=2) e[i].cap=x;
        for(int i=0; i<cc; i++) e[i].flow=0;
    
        return isap(0, n+m+1, n+m+2)==n;
    //    return Dinic(x);
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            while((getchar())!='
    ');
            memset(h, -1, sizeof(h));       cc=0;
    
            for(int u=1; u<=n; u++){
                char name[19], ch;
                int v=0;
                bool f=false;
                scanf(" %s", name);
                while((ch=getchar())!='
    '){
                    if(ch==' ' && f){
                        add(u, v+1+n, 1);
                        v=0;     f=false;
                    }
                    else if(ch!=' '){
                        v = v * 10 + ch - '0';
                        f=true;
                    }
                }
                if(f) add(u, v+1+n, 1);
            }
    
            for(int u=1; u<=n; u++) add(0, u, 1);
    
            const int tc=cc;
            for(int u=n+1; u<=m+n; u++) add(u, n+m+1, n);
    
            int l=1, r=n, mid, ans=0;
            while(l<=r){
                mid=(l+r)>>1;
                if(Dinic(mid, tc)) { ans=mid; r=mid-1; }
                else l=mid+1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
     
    
  • 相关阅读:
    剑指Offer对答如流系列
    剑指Offer对答如流系列
    KMP算法
    殊途同归
    从m个数中取top n
    用红黑树实现500万数据的动态排序
    返璞归真
    second blog编程之美------控制cpu曲线
    first blog编程之美-----计算1的个数
    mathematica入门学习记录:
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3346424.html
Copyright © 2011-2022 走看看