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

poj3281 Dining

时间:2017-04-04 11:39:57      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:like   blink   etc   mac   hint   sizeof   tle   keyword   tab   

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17181   Accepted: 7607

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.

Source

技术分享

 Select Code

#include<cstdio>
#include<cstring>
#include<iostream>
#define EF if(ch==EOF) return x;
using namespace std;
const int N=405;
const int M=1e5+5;
const int inf=2e9;
struct edge{int v,next,cap;}e[M<<1];int tot=1,head[N];
int n,m,ans,f,d,S,T,dis[N],q[N*2];
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;EF;ch=getchar();}
	while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
	return x*f;
}
void add(int x,int y,int z){
	e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot;
	e[++tot].v=x;e[tot].cap=0;e[tot].next=head[y];head[y]=tot;
}
bool bfs(){
	memset(dis,-1,sizeof dis);
	int h=0,t=1;q[t]=S;dis[S]=0;
	while(h!=t){
		int x=q[++h];
		for(int i=head[x];i;i=e[i].next){
			if(e[i].cap&&dis[e[i].v]==-1){
				dis[e[i].v]=dis[x]+1;
				if(e[i].v==T) return 1;
				q[++t]=e[i].v;
			}
		} 
	}
	return 0;
}
int dfs(int x,int f){
	if(x==T) return f;
	int used=0,t;
	for(int i=head[x];i;i=e[i].next){
		if(e[i].cap&&dis[e[i].v]==dis[x]+1){
			t=dfs(e[i].v,min(e[i].cap,f));
			e[i].cap-=t;e[i^1].cap+=t;
			used+=t;f-=t;
			if(!f) return used;
		}
	}
	if(!used) dis[x]=-1;
	return used;
}
void dinic(){
	ans=0;
	while(bfs()) ans+=dfs(S,inf);
}
void work(){
	n=read();f=read();d=read();S=0;T=f+n+n+d+1;
	for(int i=1,fi,di,x;i<=n;i++){
		fi=read();
		di=read();
		while(fi--){
			x=read();
			add(x,f+i,1);
		}
		while(di--){
			x=read();
			add(f+n+i,f+n+n+x,1);
		}
	}
	for(int i=1;i<=n;i++) add(f+i,f+n+i,1);
	for(int i=1;i<=f;i++) add(S,i,1);
	for(int i=1;i<=d;i++) add(f+n+n+i,T,1);
	dinic();
	printf("%d\n",ans);
}
int main(){
	work();
	return 0;
}

 

poj3281 Dining

标签:like   blink   etc   mac   hint   sizeof   tle   keyword   tab   

原文地址:http://www.cnblogs.com/shenben/p/6664505.html

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