POJ-3281-Dining(网络流+Dinic)

题目链接:http://poj.org/problem?id=3281

Description

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: NF, 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种饮料

对于每头牛,先输入两个数fi,di表示能吃的食物和能喝的水的数量,然后输入对应的种类。注意!每种食物||水只能被用一次;

算法还是板子,主要建图:

先说一个很显然的建图错误(如果不想看可以不看):

因为题目中说的很明确:我想当然的建立 了这样的图:很明显,牛对应的水和食物,从 源 到 汇

根据样例,第一个次输入的组合应该是:

牛一可以吃1,2,喝1,3,吃的和喝的之间可以互相组合

但是!!到了第二头牛:

图就变成了这样:

为了便于区分,我用的蓝色,其实他们时一样的,现在就发现了,原本牛一不能喝2水的,但是经过食物2后他就能喝2水了。。。肯定错了,因此要换一种建图方法:

下面是正确的建图:

中间牛和牛之间是限流用的,因为只能有一种吃法,所以中间的流量为1

对应样例输进去:

先输入三个数,发现确实可行:

然后接下来就是板子了:

ac:

//一页 27行就很舒服 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

int mp[500][500];
int vis[500],dis[500];
int n,f,d;

bool bfs(int s)
{
	clean(dis,-1);
	dis[0]=0;
	queue<int> que;
	que.push(s);
	while(que.size())
	{
		int u=que.front();
		que.pop();
		for(int i=0;i<=401;++i)
		{
			if(dis[i]<0&&mp[u][i]>0)
			{
				dis[i]=dis[u]+1;
				que.push(i);
			}
		}
	}
	if(dis[401]>0)
		return 1;
	return 0;
}

int Find(int x,int low)
{
	int a=0;
	if(x==401)
		return low;
	for(int i=0;i<=401;++i)
	{
		if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=Find(i,min(low,mp[x][i]))))
		{
			mp[x][i]=mp[x][i]-a;
			mp[i][x]=mp[i][x]+a;
			return a;
		}
	}
	return 0;
}

int main()
{
	clean(mp,0);
	cin>>n>>f>>d;
	int fi,di;
	for(int i=1;i<=n;++i)
		mp[i+100][i+200]=1;//牛 - 牛 
	for(int i=1;i<=100;++i)
		mp[0][i]=1;//源 - 食物 
	for(int i=1;i<=100;++i)
		mp[i+300][401]=1;// 水 - 汇 
	for(int i=1;i<=n;++i)//读图 
	{
		cin>>fi>>di;
		int v[300]={0};
		for(int j=0;j<fi;++j)//读食物 
			cin>>v[j];
		for(int j=0;j<di;++j)//读水源 
			cin>>v[j+100];
		for(int j=0;j<fi;++j)
			mp[v[j]][i+100]=1;//食物 - 牛 
		for(int j=0;j<di;++j)
			mp[i+200][v[j+100]+300]=1;// 牛 - 水源 
	}
	int ans=0,tmp;
	while(bfs(0))
	{
		while(tmp=Find(0,INF))
			ans=ans+tmp;
	}
	cout<<ans<<endl;
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值