您的位置:首页 > Web前端

poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

2013-08-16 18:18 447 查看
The Perfect Stall

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 16396Accepted: 7502
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

Source

USACO 40
题意很简单,就是裸的二分匹配,但是我是用最大流水过的

/*********
PRO: POJ 1274
TIT: The Perfect Stall
DAT: 2013-08-16-13.40
AUT: UKean
EMA: huyocan@163.com
*********/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define  INF 1e9
using namespace std;
queue<int> que;//广搜需要使用的队列
int s,t;//源点和汇点
int flow[505][505];//残流量
int p[505];//广搜记录路径的父节点数组
int a[505];//路径上的最小残量
int cap[505][505];//容量网络
int ans;//最大流
int read()
{
int n,m;
if(!(cin>>n>>m)) return 0;
s=0;t=m+n+1;
//1->n是牛 n+1 ->n+m是牛位
memset(cap,0,sizeof(cap));
for(int i=1;i<=n;i++)
{
cap[s][i]=1;
int si;cin>>si;
for(int j=0;j<si;j++)
{
int temp;cin>>temp;
cap[i][temp+n]=1;
}
}
for(int i=n+1;i<=n+m;i++)
cap[i][t]=1;
return 1;
}
int deal()//增广路算法就不具体解释了,详细的解释可以看我关于网络流的第一篇博客
//   http://blog.csdn.net/hikean/article/details/9918093 {
memset(flow,0,sizeof(flow));
ans=0;
while(1)
{
memset(a,0,sizeof(a));
a[s]=INF;
que.push(s);
while(!que.empty())
{
int u=que.front();que.pop();
for(int v=0;v<=t+1;v++)
if(!a[v]&&cap[u][v]-flow[u][v]>0)
{
p[v]=u;
que.push(v);
a[v]=min(a[u],cap[u][v]-flow[u][v]);//路径上的最小残流量
}
}
if(a[t]==0) break;
for(int u=t;u!=s;u=p[u])
{
flow[p[u]][u]+=a[t];
flow[u][p[u]]-=a[t];
}
ans+=a[t];
}
cout<<ans<<endl;
return ans;
}
int main()
{
while(read())
deal();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: