题目来源:http://poj.org/problem?id=1274
Time Limit: 1000MS |
Memory Limit: 10000K |
Total Submissions: 28084 |
Accepted: 12369 |
Description
Farmer John completed his new barn just lastweek, complete with all the latest milking technology. Unfortunately, due toengineering problems, all the stalls in the new barn are different. For thefirst week, Farmer John randomly assigned cows to stalls, but it quickly becameclear 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 arewilling to produce milk in which stalls. A stall may be only assigned to onecow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producingassignments 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 ofstalls in the new barn. Each of the following N lines corresponds to a singlecow. The first integer (Si) on the line is the number of stalls that the cow iswilling to produce milk in (0 <= Si <= M). The subsequent Si integers onthat line are the stalls in which that cow is willing to produce milk. The stallnumbers will be integers in the range (1..M), and no stall will be listed twicefor a given cow.
Output
For each case, output a single line with asingle integer, the maximum number of milk-producing stall assignments that canbe made.
Sample Input
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
SampleOutput
4
Source
USACO 40
-----------------------------------------------------
解题思路
匈牙利算法求二分图的最大匹配
匈牙利算法简单就是每次寻找一条增广道路
用深搜求增广道路
两个坑点:
1. 此题是多输入,要用
while(fin >> n >> m)
2. 动态开辟内存的数组头指针的size就是一个指针的size,而固定长度数组的size是整个数组的size,这点在应用memset函数给数组赋初值的时候要注意
cin >> n;
int *test1 = new int[n];
memset(test1,-1,n*sizeof(int)); // sizeof(test1) = 4
int test2[100];
memset(test2,-1,sizeof(test2)); // sizeof(test2) = 400
-----------------------------------------------------
代码
//he Perfect Stall
//Time Limit: 1000MS Memory Limit: 10000K
//Total Submissions: 28081 Accepted: 12368
//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
#include
#include
#include
using namespace std;
int dfs(int s, int n, vector*G, int *link, bool *vset) // 深搜求增广道路
{
int i= 0, t = 0;
for (i=0; i*G, int *link, bool *vset)
{
int result = 0, i = 0, j = 0;
for (i=0; i> n >> m)
{
vector *G = new vector[n+m]; // 邻接表
for (i=0; i> capa;
for (j=0; j> tmp;
tmp--;
G[i].push_back(tmp+n);
G[n+tmp].push_back(i);
}
}
int *link = new int[m]; // 从右部图到左部图的匹配
for (i=0; i> n >> m)
{
vector *G = new vector[n+m]; // 邻接表
for (i=0; i> capa;
for (j=0; j> tmp;
tmp--;
G[i].push_back(tmp+n);
G[n+tmp].push_back(i);
}
}
int *link = new int[m]; // 从右部图到左部图的匹配
for (i=0; i