码迷,mamicode.com
首页 > 其他好文 > 详细

poj3281 Dining

时间:2014-10-01 18:19:31      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   os   ar   for   strong   

---恢复内容开始---

                                                                                            Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9587   Accepted: 4426

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 Diintegers 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.

Source

 
题意:有n只奶牛,f种食物,d种饮料。给出每头牛能接受的食物和饮料,求最多可以满足多少只牛。每只牛要满足,必须要有且只有一种食物和一种饮料,每种食物或饮料只能被一只牛占据。
思路:第一次做网络流的题目,明知是网络流还是构造不出图。智商太低啊。设置一个源点和汇点,源点连向饮料,饮料流向牛,牛流向食物,食物流向汇点。注意为了让一只牛只有一种食物和一种饮料,所以还有把牛的点分成两个点,自己指向自己。一次写写得有点龊。
 
/*
 * Author:  Joshua
 * Created Time:  2014年09月30日 星期二 20时36分35秒
 * File Name: poj3281.cpp
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 405
#define maxm 60010
struct pipe 
{
	int to,cap,flow,next;
} e[maxm];
int n,d1,d2,tot,ans;
int head[maxn],cur[maxn],dis[maxn],d[maxn];
bool vis[maxn];

void build(int x,int y,int v)
{
	pipe& edge=e[tot];
	edge.to=y;
	edge.cap=v;
	edge.flow=0;
	edge.next=head[x];
	head[x]=tot;
	tot++;
	pipe& edge2=e[tot];
	edge2.to=x;
	edge2.cap=0;
	edge2.flow=0;
	edge2.next=head[y];
	head[y]=tot;
	tot++;
}

void init()
{
	int x1,x2,y;
	tot=0;
	memset(head,-1,sizeof(head));
	for (int i=1;i<=d1;++i)
		build(0,i,1);
	for (int i=1;i<=d2;++i)
		build(n*2+d1+i,n*2+d1+d2+1,1);
	for (int i=1;i<=n;++i)
		build(d1+i,d1+n+i,1);
	for (int i=1;i<=n;++i)
	{
		scanf("%d%d",&x1,&x2);
		for (int j=1;j<=x1;++j)
		{
			scanf("%d",&y);
			build(y,d1+i,1);
		}
		for (int j=1;j<=x2;++j)
		{
			scanf("%d",&y);
			build(d1+n+i,d1+n*2+y,1);
		}
	}
	n=n*2+d1+d2+1;
}

bool bfs()
{
	memset(vis,0,maxn);
	int h=1,t=1,x,y;
	dis[0]=0;
	d[1]=0;
	vis[0]=true;
	while (h<=t)
	{
		x=d[h];
		for (int i=head[x];~i;i=e[i].next)
		{
			y=e[i].to;
			if (!vis[y] && e[i].cap>e[i].flow)
			{
				d[++t]=y;
				dis[y]=dis[x]+1;
				vis[y]=true;
			}
		}
		++h;
	}
	return vis[n];
}

int dfs(int x,int a)
{
	if (x==n || a==0) return a;
	int flow=0,f;
	for (int& i=cur[x];~i;i=e[i].next)
	{
		pipe& edge=e[i];
		if (dis[x]+1==dis[edge.to] && 
			(f= dfs(edge.to, min (a,edge.cap-edge.flow)))>0)
		{
			edge.flow+=f;
			e[i^1].flow-=f;
			flow+=f;
			a-=f;
			if (a==0) break;
		}
	}
	return flow;
}

void solve()
{
	ans=0;
	while (bfs())
	{
		for (int i=0;i<=n;++i)
			cur[i]=head[i];
		ans+=dfs(0,0x7f7f7f7f);
	}
	printf("%d\n",ans);
}

int main()
{
    while (scanf("%d%d%d",&n,&d1,&d2)>0)
	{
		init();
		solve();
	}
    return 0;
}

  

poj3281 Dining

标签:des   style   blog   http   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/code-painter/p/4003391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!