1. 程式人生 > >POJ-3281 Dining (網路流 最大流 建圖) ʕ •ᴥ•ʔ

POJ-3281 Dining (網路流 最大流 建圖) ʕ •ᴥ•ʔ

Cows are such finicky eaters. Each cow has a preference for certain
foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to
check his menu against their preferences. Although he might not be
able to stuff everybody, he wants to give a complete meal of both food
and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D
(1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has
decided whether she is willing to eat a particular food or drink a
particular drink. Farmer John must assign a food type and a drink type
to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food
type 2 is assigned to a cow, no other cow can be assigned food type
2).

Input

Line 1: Three space-separated integers: N, F, and D Lines 2..N+1:
Each line i starts with a two integers Fi and Di, the number of dishes
that cow i likes and the number of drinks that cow i likes. The next
Fi integers denote the dishes that cow i will eat, and the Di integers
following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can
be fed both food and drink that conform to their wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Sample Output

3
  • 1
  • 2

Hint

One way to satisfy three cows is: Cow 1: no meal Cow 2: Food #2,
Drink #2 Cow 3: Food #1, Drink #1 Cow 4: Food #3, Drink #3 The
pigeon-hole principle tells us we can do no better since there are
only three kinds of food or drink. Other test data sets are more
challenging, of course.

思路

先說題意:

農夫為他的 N (1 ≤ N ≤ 100) 牛準備了 F (1 ≤ F ≤ 100)種食物和 D (1 ≤ D ≤ 100)
種飲料。每頭牛都有各自喜歡的食物和飲料,而每種食物或飲料只能分配給一頭牛。最多能有多少頭牛可以同時得到喜歡的食物和飲料?

因為每種食物或者每種飲料都只能分配給一頭牛,所以我們要進行拆點,來保證飲料和食物只從一條邊上面經過,不拆點不能保證牛隻能選擇一種食物和一種飲料這一條件。即限定牛結點的容量為1。那麼我們來建圖:

需要建立一個源頭,匯頭。

網路流難在建圖 然後直接敲板子

我們需要搞清楚他們各自的範圍

  • 牛:1~2∗n
  • 食物:2∗n+1~2∗n+f
  • 飲料:2∗n+f+1~2∗n+f+d
  • 超級源點:0
  • 超級匯點:2∗n+f+d+1

是不是有些迷 (反正當時我迷了) 下面我們通過圖來解釋:

1~8是牛 f 為食物 b為飲料

 然後如下圖

 

 根據題意連線各邊

 

然後就可以啦 然後直接模板! 模板最好自己存一個 模板很好理解 敲多了也就記住了

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <map>
#include<queue> 
#define ll long long
#define pi acos(-1.0)
#define N 500
using namespace std;
int x[500][500];
int vis[500];
int s,e;
int bfs()//這個bfs 讓我想起了 spfa 演算法 
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	vis[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		for(int i=1;i<=e;i++)
		{
			if(!vis[i]&&x[temp][i])
			{
				vis[i]=vis[temp]+1;
				q.push(i);
			}
		}
	}
	if(vis[e]>0)
	return 1;
	return 0;
}
int dfs(int here,int h)
{
	if(here==e)
	return h;
	int ans;
	for(int i=1;i<=e;i++)
	{
		if(x[here][i]&&vis[i]==vis[here]+1&&(ans=dfs(i,min(h,x[here][i]))))
		{
			x[here][i]-=ans;
			x[i][here]+=ans;
			return ans;
		}
	}	
	return 0;
} 
int main()
{
	int n,f,d;
	cin>>n>>f>>d;
	memset(x,0,sizeof(x));
	// 建圖 
	for(int i=1;i<=n;i++)
	{
		x[2*i-1][2*i]=1;
	}
	for(int i=1;i<=f;i++)
	{
		x[0][2*n+i]=1;
	}
	for(int i=1;i<=d;i++)
	{
		x[2*n+f+i][2*n+f+d+1]=1;
	}
	//根據要求連線對應點 
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		for(int j=1;j<=a;j++)
		{
			int c;
			cin>>c;
			x[2*n+c][2*i-1]=1;	 
		}
		for(int j=1;j<=b;j++)
		{
			int c;
			cin>>c;
			x[2*i][2*n+f+c]=1;
		}
	}
	int ans=0;
	s=0,e=2*n+f+d+1;
	while(bfs())
	{
		ans+=dfs(s,e);
	}
	cout<<ans<<endl;
	return 0;
}