食物,牛,和饮料 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;
}

 

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