程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流來水一下

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

編輯:C++入門知識

The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16396   Accepted: 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

4Source

USACO 40
題意很簡單,就是裸的二分匹配,但是我是用最大流水過的

/*********
PRO: POJ 1274
TIT: The Perfect Stall
DAT: 2013-08-16-13.40
AUT: UKean
EMA: [email protected]
*********/
#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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved