poj3281 Dining
Posted 神犇(shenben)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3281 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
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
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
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的主要内容,如果未能解决你的问题,请参考以下文章