解题报告 之 POJ1274 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.
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<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 810;
const int MAXM = 641000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, cap, next;
};
Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int score[MAXN];
int src, des, cnt;
void addedge( int from, int to, int cap )
{
edge[cnt].to = to;
edge[cnt].cap = cap;
edge[cnt].next = head[from];
head[from] = cnt++;
swap( from, to );
edge[cnt].to = to;
edge[cnt].cap = 0;
edge[cnt].next = head[from];
head[from] = cnt++;
}
int bfs( )
{
memset( level, -1, sizeof level );
queue<int> q;
while(!q.empty( ))
q.pop( );
level[src] = 0;
q.push( src );
while(!q.empty( ))
{
int u = q.front( );
q.pop( );
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > 0 && level[v] == -1)
{
level[v] = level[u] + 1;
q.push( v );
}
}
}
return level[des] != -1;
}
int dfs( int u, int f )
{
if(u == des) return f;
int tem;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > 0 && level[v] == level[u] + 1)
{
tem = dfs( v, min( f, edge[i].cap ) );
if(tem > 0)
{
edge[i].cap -= tem;
edge[i ^ 1].cap += tem;
return tem;
}
}
}
level[u] = -1;
return 0;
}
int Dinic( )
{
int ans = 0, tem;
while(bfs( ))
{
while(tem = dfs( src, INF ))
{
ans += tem;
}
}
return ans;
}
int main( )
{
int n, m;
src = 0;
des = 805;
while(cin >> n >> m)
{
memset( head, -1, sizeof head );
cnt = 0;
for(int i = 1; i <= n; i++)
addedge( src, i, 1 );
for(int j = 1; j <= m; j++)
addedge( 200 + j, des, 1 );
for(int i = 1; i <= n; i++)
{
int num;
cin >> num;
for(int j = 1; j <= num; j++)
{
int b;
cin >> b;
addedge( i, 200 + b, 1 );
}
}
cout << Dinic() << endl;
}
return 0;
}
呵呵呵呵呵呵,毛概课帮YJ大神做了个作业。。哈哈