POJ 1274 解题报告

这道题是在USACO上面做过的。当时用的是maximum flow。在这里就超时了。一个原因是usaco上面是单case,这里是多case,所以数组之类的应该重用。但主要原因还是这个是个最大二分匹配的问题,maxflow overkill了,过于重型。

似乎最大二分匹配算法又叫做匈牙利算法。geeksforgeeks上面有非常清晰的解释,本质上就是跑dfs。具体见:

http://www.geeksforgeeks.org/maximum-bipartite-matching/

thestoryofsnow1274Accepted172K0MSC++2944B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj1274 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

// See http://www.geeksforgeeks.org/maximum-bipartite-matching/ for more detailed explanation

const int MAXN = 200;
const int MAXM = 200;

// A DFS based recursive function that returns true if a
// matching for vertex u is possible
bool DFS(int u, const bool adjMatrix[][MAXM], const int N, const int M, int match[], bool visited[])
{
	// Try every job one by one
	for (int v = 0; v < M; ++v)
	{
		// If applicant u is interested in job v and v is
		// not visited
		if (adjMatrix[u][v] && !visited[v])
		{
			// Mark v as visited
			visited[v] = true;

			// If job 'v' is not assigned to an applicant OR
			// previously assigned applicant for job v (which is match[v]) 
			// has an alternate job available.
			// Since v is marked as visited in the above line, match[v] 
			// in the following recursive call will not get job 'v' again
			if (match[v] < 0 || DFS(match[v], adjMatrix, N, M, match, visited))
			{
				match[v] = u;
				return true;
			}
		}
	}

	return false;
}

// Returns maximum number of matching from N to M

// match: An array to keep track of the applicants assigned to jobs. 
// The value of match[i] is the applicant number
// assigned to job i, the value -1 indicates nobody is assigned.
int maximumBipartiteMatching(const bool adjMatrix[][MAXM], const int N, const int M, int match[], bool visited[])
{
	// Count of jobs assigned to applicants
	int mbm = 0;

	// Initially all jobs are available
	memset(match, -1, M * sizeof(int));

	for (int u = 0; u < N; ++u)
	{
		// Mark all jobs as not seen for next applicant.
		memset(visited, false, M * sizeof(bool));

		// Find if the applicant 'u' can get a job
		if (DFS(u, adjMatrix, N, M, match, visited))
		{
			mbm++;
		}
	}
	return mbm;
}

int main()
{
	int N, M;
	bool adjMatrix[MAXN][MAXM];
	int match[MAXM];
	bool visited[MAXM];

	while (scanf("%d%d", &N, &M) > 0)
	{
		for (int i = 0; i < N; ++i)
		{
			for (int j = 0; j < M; ++j)
			{
				adjMatrix[i][j] = false;
			}
		}

		for (int u = 0; u < N; ++u)
		{
			int si;
			scanf("%d", &si);
			for (int j = 0; j < si; ++j)
			{
				int v;
				scanf("%d", &v);
				v--;
				adjMatrix[u][v] = true;
			}
		}

		// printf("[debug]adjMatrix:\n");
		// for (int u = 0; u < N; ++u)
		// {
		// 	printf("%d: ", u);
		// 	for (int v = 0; v < M; ++v)
		// 	{
		// 		printf("%d ", adjMatrix[u][v]);
		// 	}
		// 	printf("\n");
		// }

		int mbm = maximumBipartiteMatching(adjMatrix, N, M, match, visited);
		printf("%d\n", mbm);
	}
	
	return 0;  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值