食物,牛,和飲料 Dining 最大流 拆點 (Dinic)

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

Sample Output

3

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頭牛,F種食物和D種飲料,每頭牛有多種喜歡的食物和飲料,每頭牛只可以吃一種食物和飲料,且每種食物和飲料都只能被一頭牛吃掉。一頭牛滿意當且僅當它吃到滿意的食物並且喝到想喝的飲料,問最多可能讓多少頭牛滿意。
題解:
把每頭牛拆成兩個點x和x+n,給x和x+n連一條容量爲1的邊
如果一頭牛x喜歡一種食物,那麼給x和食物編號點連一條容量爲1的邊
如果一頭牛x喜歡一種飲料,那麼給x+n和飲料編號點連一條容量爲1的邊
食物編號點和點S之間分別連一條容量爲1的邊
飲料編號點和點T之間分別連一條容量爲1的邊
然後dinic就過啦!

代碼:

//建邊的時候一定要注意!!! 
//網絡流中的邊是有向的!!! 
#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
#include<cmath>
using namespace std;

const int maxv=1e6+10, maxn=1e2+10, INF=1e9;

struct edge{int to, cap, rev;};
vector<edge> g[maxv];
bool used[maxv];

void add_edge(int from,int to,int cap)
{
	g[from].push_back((edge){to, cap, g[to].size()});
	g[to].push_back((edge){from, 0, g[from].size()-1});
}

int dfs(int v, int t, int f)
{
	if (v==t) return f;
	used[v]=true;
	for (int i=0; i<g[v].size(); i++)
	{
		edge &e=g[v][i];
		if (!used[e.to] && e.cap>0){
			int d=dfs(e.to, t, min(f, e.cap));
			if (d>0){
				e.cap -= d;
				g[e.to][e.rev].cap+=d;
				return d;
			}			
		}
	}	
	return 0;
}

int max_flow(int s, int t){
	int flow=0;
	for (;;){
		memset(used, 0, sizeof(used));
		int f=dfs(s, t, INF);
		if (f == 0) return flow;
		flow += f;
	} 
} 

bool f[105][105],d[105][105];
int main()
{
	int n,fi,di,ff,dd,x,y,si,ti;
	
	memset(f,0,sizeof(f));
	memset(d,0,sizeof(d));
	scanf("%d%d%d", &n, &fi, &di);
		
	si=n*2+fi+di; ti=n*2+fi+di+1;

	for (int i=0; i<=ti; i++)
		g[i].clear();
		
	for (int i=0; i<n; i++)
	{
		scanf("%d%d", &ff, &dd);
		
		for (int j=1; j<=ff; j++) 
		{
			scanf("%d", &x);
			f[i][x-1]=1;
		}
		
		for (int j=1; j<=dd; j++){
			scanf("%d", &y);
			d[i][y-1]=1;
		}
	}
	
	for (int i=0; i<n; i++)
	  {
		  	add_edge(i, i+n, 1);
		  }
	  	
	for (int i=0; i<fi; i++)
	{
		add_edge(si, 2*n+i, 1);
		
		  }
		
	for (int i=0; i<di; i++)
	{
			add_edge(2*n+fi+i, ti, 1);
	}
	
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<fi; j++)
		if (f[i][j])
		{
			add_edge(2*n+j, i, 1);
		}
			
		for (int j=0; j<di; j++)
		if (d[i][j])
		{	
		 	add_edge(n+i, 2*n+fi+j, 1);
		}
	}
	
	cout<<max_flow(si,ti);
	
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章