• nefu 474 The Perfect StallHal Burch 二分图最大匹配


    The Perfect StallHal Burch

    Time Limit 1000ms

    Memory Limit 65536K

    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

    Input file contains multiple test cases. 
    In a test case:
    Line 1:	One line with 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.
    Line 2..N+1:	N lines, each corresponding 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,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

    从0到1...n建立容量为1的边,从1...n到n+1...n+m根据题目数据建容量为1的边,从n+1...n+m到n+m+1建立容量为1的边。

    源点为0,汇点为n+m+1,求最大流。

    注意边要开的大一点。

    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    const int OO=1e9;
    const int maxn=444;
    const int maxm=111111;
    
    int n,m;
    
    struct EDGE{
        int to;
        int flow;
        int next;
    }edges[maxm];
    int node,src,dest,edge;
    int head[maxn],work[maxn],dis[maxn],q[maxn];
    
    void addedge(int u,int v,int c);
    void prepare(int _node,int _src,int _dest);
    int Dinic_flow();
    int Dinic_dfs(int u,int exp);
    bool Dinic_bfs();
    
    void prepare(int _node,int _src,int _dest)
    {
        node=_node;
        src=_src;
        dest=_dest;
        for (int i=0;i<=node;i++) head[i]=-1;
        edge=0;
    }
    
    void addedge(int u,int v,int c)
    {
        edges[edge].flow=c;edges[edge].to=v;edges[edge].next=head[u];head[u]=edge++;
        edges[edge].flow=0;edges[edge].to=u;edges[edge].next=head[v];head[v]=edge++;
    }
    
    int Dinic_flow()
    {
        int ret=0,tmp;
        while (Dinic_bfs())
        {
            for (int i=0;i<node;i++) work[i]=head[i];
            while (tmp=Dinic_dfs(src,OO)) ret+=tmp;
        }
        return ret;
    }
    
    bool Dinic_bfs()
    {
        int u,v,r=0;
        for (int i=0;i<node;i++) dis[i]=-1;
        q[r++]=src;
        dis[src]=0;
        for (int l=0;l<r;l++)
        {
            u=q[l];
            for (int i=head[u];i!=-1;i=edges[i].next)
            {
                v=edges[i].to;
                if (edges[i].flow&&dis[v]<0)
                {
                    dis[v]=dis[u]+1;
                    q[r++]=v;
                    if (v==dest) return true;
                }
            }
        }
        return false;
    }
    
    int Dinic_dfs(int u,int exp)
    {
        int v,tmp;
        if (u==dest) return exp;
        for (int &i=work[u];i!=-1;i=edges[i].next)
        {
            v=edges[i].to;
            if (edges[i].flow&&dis[v]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,edges[i].flow))>0))
            {
                edges[i].flow-=tmp;
                edges[i^1].flow+=tmp;
                return tmp;
            }
        }
        return 0;
    }
    
    
    int main()
    {
        while (~scanf("%d%d",&n,&m))
        {
            prepare(n+m+2,0,n+m+1);
            for (int i=1;i<=n;i++)
            {
                addedge(src,i,1);
                int t;
                scanf("%d",&t);
                for (int j=1;j<=t;j++)
                {
                    int v;
                    scanf("%d",&v);
                    addedge(i,n+v,1);
                }
            }
            for (int i=n+1;i<=n+m;i++)
            {
                addedge(i,dest,1);
            }
            printf("%d\n",Dinic_flow());
        }
        return 0;
    }
    



  • 相关阅读:
    WCF Restful调用跨域解决方案
    [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
    人体呼吸信号的数据挖掘
    Spark编译及spark开发环境搭建
    诗两首------重庆项目出差有感
    eclipse安装和中文汉化,以及配置
    Querying CRM data with LINQ
    oracle pl/sql之在java中怎么调用oracle函数
    oracle pl/sql之oracle函数
    oracle pl/sql之java中调用oracle有参存储过程
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038448.html
Copyright © 2020-2023  润新知