Language:
Dining
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: N, F, 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 theDi 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.
这是一道拆点题,要把牛拆点,主要就是因为每头牛最多只能喂饱一次,然后就是纯粹的最大流了,下面是ac代码:
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<climits> using namespace std; struct Edge { int s,t,c,next; }edge[1010000]; int head[440]; int pre[440]; int n,f,d; int fi,di,fc[110],dc[110],s,t; int Min(int a,int b) { return a<b?a:b; } void init() { int ent=0; memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&f,&d); s=0;t=f+d+2*n+1; for(int i=1;i<=f;i++) { edge[ent].s=0;edge[ent].t=i;edge[ent].c=1;edge[ent].next=head[0];head[0]=ent++; edge[ent].s=i;edge[ent].t=0;edge[ent].c=0;edge[ent].next=head[i];head[i]=ent++; } for(int i=1;i<=d;i++) { edge[ent].s=f+i;edge[ent].t=t;edge[ent].c=1;edge[ent].next=head[f+i];head[f+i]=ent++; edge[ent].s=t;edge[ent].t=f+i;edge[ent].c=0;edge[ent].next=head[t];head[t]=ent++; } for(int i=f+d+1;i<=f+d+n;i++) { edge[ent].s=i;edge[ent].t=i+n;edge[ent].c=1;edge[ent].next=head[i];head[i]=ent++; edge[ent].s=i+n;edge[ent].t=i;edge[ent].c=0;edge[ent].next=head[i+n];head[i+n]=ent++; } for(int i=1;i<=n;i++) { int cow=f+d+i; scanf("%d%d",&fi,&di); for(int j=0;j<fi;j++) { scanf("%d",&fc[j]); int ok=1; for(int k=head[fc[j]];k!=-1;k=edge[k].next) { if(edge[k].t==cow) { ok=0;break; } } if(ok) { edge[ent].s=fc[j];edge[ent].t=cow;edge[ent].c=1;edge[ent].next=head[fc[j]];head[fc[j]]=ent++; edge[ent].s=cow;edge[ent].t=fc[j];edge[ent].c=0;edge[ent].next=head[cow];head[cow]=ent++; } } for(int j=0;j<di;j++) { scanf("%d",&dc[j]); dc[j]+=f; int ok=1; for(int k=head[cow];k!=-1;k=edge[k].next) { if(edge[k].t==dc[j]) { ok=0;break; } } if(ok) { edge[ent].s=cow+n;edge[ent].t=dc[j];edge[ent].c=1;edge[ent].next=head[cow+n];head[cow+n]=ent++; edge[ent].s=dc[j];edge[ent].t=cow+n;edge[ent].c=0;edge[ent].next=head[dc[j]];head[dc[j]]=ent++; } } } } bool bfs() { memset(pre,-1,sizeof(pre)); pre[s]=0; queue<int>q; q.push(s); while(!q.empty()) { int temp=q.front(); q.pop(); for(int i=head[temp];i!=-1;i=edge[i].next) { int temp2=edge[i].t; if(pre[temp2]==-1&&edge[i].c) { pre[temp2]=pre[temp]+1; q.push(temp2); } } } return pre[t]!=-1; } int dfs(int start,int minn) { if(start==t)return minn; int flow=0; int small; for(int i=head[start];i!=-1;i=edge[i].next) { int temp=edge[i].t; if(pre[temp]==pre[start]+1&&edge[i].c) { small=Min(minn-flow,edge[i].c); small=dfs(temp,small); flow+=small; edge[i].c-=small; edge[i^1].c+=small; } } return flow; } void dinic() { int flow=0; while(bfs()) { flow+=dfs(s,INT_MAX); } printf("%d\n",flow); } int main() { init(); dinic(); return 0; }
|