poj 1274(网络流解二分图的最大匹配)

Posted AC菜鸟机

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 1274(网络流解二分图的最大匹配)相关的知识,希望对你有一定的参考价值。

The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22539   Accepted: 10072

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

题意:农夫养了n头牛,建了m个牲口棚,他发现每头牛只在特定的一些牲口棚才能产奶,问最多有多少头牛能够同时产奶??
题解:二分图的最大匹配,设立一个超级源点,向每头牛连一条长度为1的单向边,建立超级汇点,每个牲口棚向超级汇点连一条长度为1的单向边,然后每头牛向其喜爱的牲口棚连一条长度不小于1的边,求最大流即可。
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
const int N = 505;
const int INF = 999999999;
struct Edge{
    int v,w,next;
}edge[N*N];
int head[N];
int level[N];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k){
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des){
    queue<int >q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v,w=edge[k].w;
            if(level[v]==0&&w!=0){
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des) return increaseRoad;
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            edge[k].w -=w;
            edge[k^1].w+=w;
            ret+=w;
            if(ret==increaseRoad) return ret;
        }
    }
    return ret;
}
int Dinic(int src,int des){
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        int src = 0,des = n+m+1;
        for(int i=1;i<=n;i++){
            addEdge(src,i,1,tot);
            int t,v;
            scanf("%d",&t);
            while(t--){
                scanf("%d",&v);
                addEdge(i,v+n,1,tot);
            }
        }
        for(int i=n+1;i<=n+m;i++){
            addEdge(i,des,1,tot);
        }
        printf("%d\n",Dinic(src,des));
    }
}

 

以上是关于poj 1274(网络流解二分图的最大匹配)的主要内容,如果未能解决你的问题,请参考以下文章

最小点覆盖,二分图最大匹配—POJ1274 POJ1469 POJ1469

[题解]poj 1274 The Perfect Stall(网络流)

poj1274 二分匹配

POJ 1274 The Perfect StallHDU 2063 过山车(最大流做二分匹配)

POJ1274 The Perfect Stall 二分图,匈牙利算法

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)