首页 > 代码库 > poj3281 Dining
poj3281 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
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
Sample Input
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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
/* * 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 60010struct 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