POJ3281 Dining
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 DLines 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 Fiintegers denote the dishes that cow i will eat, and the Di 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 wishesSample 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. 题意:n头奶牛,f种食物,d种饮料。每种食物,饮料只有一份。每头奶牛需要一份食物和饮料,求满足奶牛需求的最大数量。 建图:食物与源点相连,流量为1,奶牛拆开,控制流量为1,然后奶牛与水连接,流量为1。跑最大流即可
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 using namespace std; 7 8 const int maxn=100010; 9 const int maxm=400010; 10 const int inf=0x3f3f3f3f; 11 struct Edge{ 12 int to,next,cap,flow,cost; 13 }edge[maxm]; 14 15 int tol; 16 int head[maxn]; 17 int gap[maxn],dep[maxn],cur[maxn]; 18 void init() { 19 tol=0; 20 memset(head,-1,sizeof(head)); 21 } 22 void addedge(int u,int v,int w,int rw=0) { 23 edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0; 24 edge[tol].next=head[u];head[u]=tol++; 25 edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0; 26 edge[tol].next=head[v];head[v]=tol++; 27 } 28 29 int Q[maxn]; 30 void bfs(int start,int end) { 31 memset(dep,-1,sizeof(dep)); 32 memset(gap,0,sizeof(gap)); 33 gap[0]=1; 34 int front=0,rear=0; 35 dep[end]=0; 36 Q[rear++]=end; 37 while(front!=rear) { 38 int u=Q[front++]; 39 for(int i=head[u];i!=-1;i=edge[i].next) { 40 int v=edge[i].to; 41 if(dep[v]!=-1) continue; 42 Q[rear++]=v; 43 dep[v]=dep[u]+1; 44 gap[dep[v]]++; 45 } 46 } 47 } 48 49 int S[maxn]; 50 int sap(int start,int end,int n) { 51 bfs(start,end); 52 memcpy(cur,head,sizeof(head)); 53 int top=0; 54 int u=start; 55 int ans=0; 56 while(dep[start]<n) { 57 if(u==end) { 58 int minn=inf; 59 int inser; 60 for(int i=0;i ) { 61 if(minn>edge[S[i]].cap-edge[S[i]].flow) { 62 minn=edge[S[i]].cap-edge[S[i]].flow; 63 inser=i; 64 } 65 } 66 for(int i=0;i ) { 67 edge[S[i]].flow+=minn; 68 edge[S[i]^1].flow-=minn; 69 } 70 ans+=minn; 71 top=inser; 72 u=edge[S[top]^1].to; 73 continue; 74 } 75 bool flag=false; 76 int v; 77 for(int i=cur[u];i!=-1;i=edge[i].next) { 78 v=edge[i].to; 79 if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) { 80 flag=true; 81 cur[u]=i; 82 break; 83 } 84 } 85 if(flag) { 86 S[top++]=cur[u]; 87 u=v; 88 continue; 89 } 90 int minn=n; 91 for(int i=head[u];i!=-1;i=edge[i].next) { 92 if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<minn) { 93 minn=dep[edge[i].to]; 94 cur[u]=i; 95 } 96 } 97 gap[dep[u]]--; 98 if(!gap[dep[u]]) return ans; 99 dep[u]=minn+1; 100 gap[dep[u]]++; 101 if(u!=start) u=edge[S[--top]^1].to; 102 } 103 return ans; 104 } 105 106 int main() { 107 int n,f,d; 108 while(~scanf("%d%d%d",&n,&f,&d)) { 109 init(); 110 for(int i=1;i<=f;i++) { 111 addedge(0,i,1); 112 } 113 for(int i=1;i<=n;i++) { 114 int num1,num2; 115 scanf("%d%d",&num1,&num2); 116 int x; 117 while(num1--) { 118 scanf("%d",&x); 119 addedge(x,f+i,1); 120 } 121 while(num2--) { 122 scanf("%d",&x); 123 addedge(f+n+i,f+n*2+x,1); 124 } 125 addedge(f+i,f+n+i,1); 126 } 127 for(int i=1;i<=d;i++) { 128 addedge(f+n*2+i,f+n*2+d+1,1); 129 } 130 printf("%d\n",sap(0,f+n*2+d+1,f+n*2+d+2)); 131 } 132 }