• 二分图最大匹配(匈牙利算法)


    http://poj.org/problem?id=1274

    The Perfect Stall
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 17672   Accepted: 8060

    Description

    Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
    Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 

    Input

    The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

    Output

    For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

    Sample Input

    5 5
    2 2 5
    3 2 3 4
    2 1 5
    3 1 2 5
    1 2 
    

    Sample Output

    4
    题意:

    一只牛对应一个房间,一个房间住一只牛

    问最大的匹配数:

    方法一:

    最大流

    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"queue"
    #define M 10009
    #define inf 999999999
    using namespace std;
    struct st
    {
        int u,v,w,next;
    }edge[M];
    int t,head[M],use[M],pre[M],dis[M];
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,int w)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
    
        edge[t].u=v;
        edge[t].v=u;
        edge[t].w=0;
        edge[t].next=head[v];
        head[v]=t++;
    }
    int BFS(int S,int T)
    {
        int i;
        queue<int>q;
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        q.push(S);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(edge[i].w&&dis[v]==-1)
                {
                    dis[v]=dis[u]+1;
                    if(v==T)
                        return 1;
                    q.push(v);
                }
            }
        }
        return 0;
    }
    int DFS(int S,int a,int T)
    {
        if(S==T)
            return a;
        for(int i=head[S];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]==dis[S]+1)
            {
                int tt=DFS(v,min(a,edge[i].w),T);
                if(tt)
                {
                    edge[i].w-=tt;
                    edge[i^1].w+=tt;
                    return tt;
                }
            }
        }
        return 0;
    }
    int solve(int S,int T)
    {
        int ans=0;
        while(BFS(S,T))
        {
            while(int tt=DFS(S,inf,T))
                ans+=tt;
        }
        return ans;
    }
    <pre name="code" class="cpp">/*int BFS(int S,int T)
    {
        queue<int>q;
        memset(pre,-1,sizeof(pre));
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        q.push(S);
        int i;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(edge[i].w&&dis[v]==-1)
                {
                    pre[v]=i;
                    dis[v]=dis[u]+1;
                    if(v==T)
                    {
                        return 1;
                    }
                    q.push(v);
                }
            }
        }
        return 0;
    }
    int solve(int S,int T)
    {
        int ans=0,i;
        while(BFS(S,T))
        {
            int mini=inf;
            for(i=pre[T];i!=-1;i=pre[edge[i].u])
            {
                mini=min(mini,edge[i].w);
            }
            for(i=pre[T];i!=-1;i=pre[edge[i].u])
            {
                edge[i].w-=mini;
                edge[i^1].w+=mini;
            }
            ans+=mini;
        }
        return ans;
    }*/

    
    
    int main()
    {
        int n,m,i,k;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            init();
            for(i=1;i<=n;i++)
            {
                scanf("%d",&k);
                while(k--)
                {
                    int b;
                    scanf("%d",&b);
                    add(i,b+n,1);
                }
            }
            for(i=1;i<=n;i++)
                add(0,i,1);
            for(i=1;i<=m;i++)
                add(i+n,m+n+1,1);
            int ans=solve(0,m+n+1);
            printf("%d
    ",ans);
        }
        return 0;
    }
    方法二:

    二分图最大匹配

    程序:

    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"queue"
    #define M 10009
    #define inf 999999999
    using namespace std;
    struct st
    {
        int u,v,next;
    }edge[M];
    int head[M],use[M],t,x[M],y[M];
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].next=head[u];
        head[u]=t++;
    }
    int finde(int u)
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(!use[v])
            {
                use[v]=1;
                if(!y[v]||finde(y[v]))
                {
                    use[v]=1;
                    y[v]=u;
                    x[u]=v;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        int n,m,i,k;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            init();
            for(i=1;i<=n;i++)
            {
                scanf("%d",&k);
                while(k--)
                {
                    int b;
                    scanf("%d",&b);
                    add(i,b+n);
                }
            }
            int ans=0;
            memset(x,0,sizeof(x));
            memset(y,0,sizeof(y));
            for(i=1;i<=n;i++)
            {
                if(!x[i])
                {
                    memset(use,0,sizeof(use));
                    ans+=finde(i);
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    



  • 相关阅读:
    深入理解Java:注解(Annotation)--注解处理器
    深入理解Java:注解(Annotation)基本概念
    深入理解Java:注解(Annotation)自定义注解入门
    SpringMVC从Controller跳转到另一个Controller
    使用 Spring 2.5 注释驱动的 IoC 功能
    SpringMVC之controller篇
    Oracle存储过程in、out、in out 模式参数
    初识Flutter
    浅谈Android 6.0之Runtime Permissions
    Java版斯诺克开源分享
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348223.html
Copyright © 2020-2023  润新知