poj 1274: ThePerfect Stall (匈牙利算法求最大匹配)

题目来源:http://poj.org/problem?id=1274

ThePerfect Stall

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<fstream>
#include<iostream>
#include<vector>
using namespace std;

int dfs(int s, int n, vector<int>*G, int *link, bool *vset)	// 深搜求增广道路
{
	int i= 0, t = 0;
	for (i=0; i<G[s].size(); i++)
	{
		t = G[s].at(i);							// G[s]: s的后继节点集
		if (!vset[t-n])							// t没有被访问过
		{
			vset[t-n] = true;						// t被访问了
			if (link[t-n]==-1 || dfs(link[t-n],n,G,link,vset))	// 存在一条增广路径(递归深搜)
			{
				link[t-n] = s;
				return 1;
			}
		}
	}
	return 0;
}

int match(int n, int m, vector<int>*G, int *link, bool *vset)
{
	int result = 0, i = 0, j = 0;
	for (i=0; i<n; i++)
	{
		for (j=0; j<m; j++)
		{
			vset[j] = false;
		}
		result += dfs(i,n,G,link,vset);			// 对左半部图的每一个节点求增广道路
	}
	return result;
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("cow.txt");
	int n,m,i,j,capa,tmp;
	while(fin >> n >> m)
	{
		vector<int> *G = new vector<int>[n+m];			// 邻接表
		for (i=0; i<m+n; i++)
		{
			G[i].clear();
		}

		for (i=0; i<n; i++)
		{
			fin >> capa;
			for (j=0; j<capa; j++)
			{
				fin >> tmp;
				tmp--;
				G[i].push_back(tmp+n);
				G[n+tmp].push_back(i);
			}
		}
		int *link = new int[m];							// 从右部图到左部图的匹配
		for (i=0; i<m; i++)
		{
			link[i] = -1;								// -1表示该节点尚未匹配
		}
		bool *vset = new bool[m];						// 在该轮迭代中该节点是否被访问过
		for (i=0; i<m; i++)
		{
			vset[i] = false;
		}
		int ans = match(n,m,G,link,vset);
		cout << ans << endl;
		delete[] G;
		delete[] link;
		delete[] vset;
	}
	fin.close();
#endif
#ifdef ONLINE_JUDGE
	int n,m,i,j,capa,tmp;
	while(cin >> n >> m)
	{
		vector<int> *G = new vector<int>[n+m];			// 邻接表
		for (i=0; i<m+n; i++)
		{
			G[i].clear();
		}

		for (i=0; i<n; i++)
		{
			cin >> capa;
			for (j=0; j<capa; j++)
			{
				cin >> tmp;
				tmp--;
				G[i].push_back(tmp+n);
				G[n+tmp].push_back(i);
			}
		}
		int *link = new int[m];							// 从右部图到左部图的匹配
		for (i=0; i<m; i++)
		{
			link[i] = -1;								// -1表示该节点尚未匹配
		}
		bool *vset = new bool[m];						// 在该轮迭代中该节点是否被访问过
		for (i=0; i<m; i++)
		{
			vset[i] = false;
		}
		int ans = match(n,m,G,link,vset);
		cout << ans << endl;
		delete[] G;
		delete[] link;
		delete[] vset;
	}
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80299107