POJ 1274 The Perfect Stall 网络流 二分图匹配

题目描述:

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

题目分析:
n头牛,m个墙,每头牛有若干个可以分配的墙,求最多分配的牛。
网络流做法是设源点s=0,汇点t=n+m+1,牛与s建边,墙与t建边,牛与可以连通的墙建边,跑一边最大流。
二分图匹配做法就是匈牙利算法,几乎也是模板。

代码如下:

网络流做法:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

const int INF=0x3f3f3f3f;
const int MAXN=1010;
using namespace std;
struct node
{
    int v;
    int w;
    int next;
}mp[MAXN<<5];
int id;
int q[MAXN];
int level[MAXN];
int head[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u,int v,int w)
{
    mp[id].v=v;
    mp[id].w=w;
    mp[id].next=head[u];
    head[u]=id++;

    mp[id].v=u;
    mp[id].w=0;
    mp[id].next=head[v];
    head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
    memset(level,0,sizeof(level));
    level[s]=1;
    int front=0,rear=1;
    q[front]=s;
    while(front < rear)
    {
        int x=q[front++];
        if(x==t) return 1;
        for(int e=head[x]; e!=-1; e=mp[e].next)
        {
            int v=mp[e].v, f=mp[e].w;
            if(!level[v] && f)
            {
                level[v]=level[x]+1;
                q[rear++]=v;
            }
        }
    }
    return 0;
}

int dfs(int u,int maxf,int t)
{
    if(u==t) return maxf;
    int ret=0;
    for(int e=head[u]; e!=-1; e=mp[e].next)
    {
        int v=mp[e].v, f=mp[e].w;
        if(level[u]+1==level[v] && f)
        {
            int Min=min(maxf-ret,f);
            f=dfs(v,Min,t);
            mp[e].w-=f;
            mp[e^1].w+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}

int dinic(int s, int t) //s源点 t汇点
{
    int ans = 0;
    while(bfs(s,t))
       ans+=dfs(s,INF,t);
    return ans;
}

int main()
{
    int n,m;
    int s,t;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        init();
        s=0;
        t=n+m+1;
        for(int i=1; i<=n; i++)
        {
            addedge(s,i,1);
            int k;
            scanf("%d",&k);
            while(k--)
            {
                int v;
                scanf("%d",&v);
                addedge(i,v+n,1);
            }
        }
        for(int i=1; i<=m; i++)
        {
            addedge(n+i,t,1);
        }
        int ans=dinic(s,t);
        printf("%d\n",ans);
    }
    return 0;
}

二分图匹配算法:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;
const int MAXN=220;
int uN,vN;
bool mp[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
    for(int v=1; v<=vN; v++)
      if(mp[u][v] && !used[v])
      {
          used[v]=true;
          if(linker[v]==0 || dfs(linker[v]))
          {
              linker[v]=u;
              return true;
          }
      }
    return false;
}
int hungary()
{
    int ret=0;
    memset(linker,0,sizeof(linker));
    for(int u=1; u<=uN; u++)
    {
        memset(used,false,sizeof(used));
        if(dfs(u)) ret++;
    }
    return ret;
}




int main()
{
     while(~scanf("%d%d",&uN,&vN))
     {
         memset(mp,0,sizeof(mp));
         for(int i=1;i<=uN;i++)
         {
             int k;
             scanf("%d",&k);
             while(k--)
             {
                 int v;
                 scanf("%d",&v);
                 mp[i][v]=1;
             }
         }
         printf("%d\n",hungary());
     }
     return 0;
}

你可能感兴趣的:(网络流,最大二分图匹配)