poj3281 Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14316 | Accepted: 6491 |
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
【题目大意】
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100)
【建模方法】
此题的建模方法比较有开创性。以往一般都是左边一个点集表示供应并与源相连,右边一个点集表示需求并与汇相连。现在不同了,供应有两种资源,需求仍只有一个群体,怎么办?其实只要仔细思考一下最大流的建模原理,此题的构图也不是那么难想。最大流的正确性依赖于它的每一条s-t流都与一种实际方案一一对应。那么此题也需要用s-t流将一头牛和它喜欢的食物和饮料“串”起来,而食物和饮料之间没有直接的关系,自然就想到把牛放在中间,两边是食物和饮料,由s, t将它们串起来构成一种分配方案。至此建模的方法也就很明显了:每种食物i作为一个点并连边(s, i, 1),每种饮料j作为一个点并连边(j, t, 1),将每头牛k拆成两个点k’, k’’并连边(k’, k’’, 1), (i, k’, 1), (k’’, j, 1),其中i, j均是牛k喜欢的食物或饮料。求一次最大流即为结果。(引自《网络流建模汇总》by Edelweiss)
#include<cstdio> #include<cstring> #include<iostream> #define INF 0x3f3f3f3f using namespace std; int N,F,D,s,t,ecnt,first[20100],nxt[20100]; struct Edge{int u,v,cap,flow;}e[20100]; bool vis[20100]; int p[20100],q[20100],d[20100],num[20100],cur[20100]; void Link(int a,int b,int c) { e[++ecnt].u=a,e[ecnt].v=b,e[ecnt].cap=c,e[ecnt].flow=0; nxt[ecnt]=first[a],first[a]=ecnt; e[++ecnt].u=b,e[ecnt].v=a,e[ecnt].cap=0,e[ecnt].flow=0; nxt[ecnt]=first[b],first[b]=ecnt; } void bfs() { memset(vis,false,sizeof(vis)); int head=0,tail=1; q[0]=t,d[t]=0,vis[t]=true; while(head^tail){ int now=q[head++]; for(int i=first[now];i;i=nxt[i]) if(!vis[e[i].u]&&e[i].cap>e[i].flow){ vis[e[i].u]=true; d[e[i].u]=d[now]+1; q[tail++]=e[i].u; } } } int Agument() { int x=t,a=INF; while(x^s){ a=min(a,e[p[x]].cap-e[p[x]].flow); x=e[p[x]].u; } x=t; while(x^s){ e[p[x]].flow+=a; e[p[x]^1].flow-=a; x=e[p[x]].u; } return a; } int ISAP() { int flow=0; bfs(); memset(num,0,sizeof(num)); for(int i=0;i<=N*2+F+D+1;i++)num[d[i]]++; int x=s; for(int i=0;i<=N*2+F+D+1;i++)cur[i]=first[i]; while(d[s]<N*2+F+D+2){ if(!(x^t)){ flow+=Agument(); x=s; } bool advanced=false; for(int i=cur[x];i;i=nxt[i]) if(e[i].cap>e[i].flow&&d[x]==d[e[i].v]+1){ advanced=true; p[e[i].v]=i; cur[x]=i; x=e[i].v; break; } if(!advanced){ int mn=N*2+F+D; for(int i=first[x];i;i=nxt[i]) if(e[i].cap>e[i].flow) mn=min(mn,d[e[i].v]); if(--num[d[x]]==0)break; num[d[x]=mn+1]++; cur[x]=first[x]; if(x^s)x=e[p[x]].u; } } return flow; } int main() { while(~scanf("%d%d%d",&N,&F,&D)){ s=0,t=N*2+F+D+1,ecnt=1; memset(first,0,sizeof(first)); memset(nxt,0,sizeof(nxt)); memset(p,0,sizeof(p)); for(int i=1;i<=F;i++)Link(s,i,1); for(int a,b,i=1;i<=N;i++){ scanf("%d%d",&a,&b); for(int x;a;a--){ scanf("%d",&x); Link(x,i+F,1); } Link(i+F,i+N+F,1); for(int x;b;b--){ scanf("%d",&x); Link(i+N+F,x+F+N*2,1); } } for(int i=1+F+N*2;i<=D+F+N*2;i++)Link(i,t,1); printf("%d\n",ISAP()); } return 0; } /*反思:牛点要拆,不然会出现一头牛吃多份餐的情况;*/
15722550
|