Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17734 | Accepted: 7908 |
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
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
Source
题意:n头牛,f种食物,d种饮料,
每头牛有喜欢的食物、饮料,
每头牛只能分配一种食物和一种饮料
问最后最多能满足多少头牛
题解:把牛拆点,放在中间,左边食物,右边饮料,最大流
#include#include #include #define N 501#define M N*Nusing namespace std;int src,decc,ans;int from[N],to[M],nxt[M],tot=1;int cap[M];int cur[N],lev[N];queue q;void add(int u,int v,int f){ to[++tot]=v; nxt[tot]=from[u]; from[u]=tot; cap[tot]=f; to[++tot]=u; nxt[tot]=from[v]; from[v]=tot; cap[tot]=0;}bool bfs(){ while(!q.empty()) q.pop(); for(int i=src;i<=decc;i++) cur[i]=from[i],lev[i]=-1; q.push(src); lev[src]=0; int now; while(!q.empty()) { now=q.front(); q.pop(); for(int i=from[now];i;i=nxt[i]) { if(lev[to[i]]==-1&&cap[i]>0) { lev[to[i]]=lev[now]+1; if(to[i]==decc) return true; q.push(to[i]); } } } return false;}int dinic(int now,int flow){ if(now==decc) return flow; int rest=0,delta; for(int &i=cur[now];i;i=nxt[i]) { if(lev[to[i]]>lev[now]&&cap[i]>0) { delta=dinic(to[i],min(cap[i],flow-rest)); if(delta) { cap[i]-=delta; cap[i^1]+=delta; rest+=delta; if(rest==flow) break; } } } if(rest!=flow) lev[now]=-1; return rest;}int main(){ int n,f,d,fi,di,x; scanf("%d%d%d",&n,&f,&d); decc=(n<<1|1)+f+d+1; for(int i=1;i<=f;i++) add(src,(n<<1|1)+i,1); for(int i=1;i<=d;i++) add((n<<1|1)+f+i,decc,1); for(int i=1;i<=n;i++) add(i<<1,i<<1|1,1); for(int i=1;i<=n;i++) { scanf("%d%d",&fi,&di); for(int j=1;j<=fi;j++) { scanf("%d",&x); add((n<<1|1)+x,i<<1,1); } for(int j=1;j<=di;j++) { scanf("%d",&x); add(i<<1|1,(n<<1|1)+f+x,1); } } while(bfs()) ans+=dinic(src,N); printf("%d",ans);}
错误:拆点方式:i<<1,i<<1|1,所以最后一头牛的编号为n<<1|1