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;
    }
    
     
    
  • 相关阅读:
    [EULAR文摘] 超声腱鞘炎对RA早期诊断的价值
    [EULAR文摘] 脊柱放射学持续进展是否显著影响关节功能
    第24周SDAI缓解能否预测远期RA骨破坏受抑制
    比较多普勒超声与临床缓解标准对RA放射学进展的预测效能
    血药谷浓度能否区分经TNF拮抗剂诱导获得缓解和低活动度的RA患者
    MRI病变能否预测已获临床缓解的早期RA未来放射学进展
    生物制剂时代的SpA研究正站在十字路口_Appel,Sieper2009
    【分享】操作配置文件帮助类
    MongoDB学习笔记-创建、更新、删除文档
    缓存学习笔记-1
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3346424.html
Copyright © 2011-2022 走看看