Very ingenious thought.
Transferred from: http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html
I can think of the maximum flow to do, it is really too absolute. The modeling method is wonderful!
test instructions is n cows, f food, D drinks.
n cows have a certain preference for each cow, and only a few foods and drinks are preferred.
each food and drink can only be given to a cow. A cow can only get one food and drink.
and a cow must have both a food and a drink to satisfy. Ask at most how many cows can be fulfilled.
The first is quite a binary match. But obviously not, because to allocate two things, two things to be satisfied at the same time.
The maximum flow chart is to put food and drink on both ends. A cow is split into two points, and the capacity between two points is 1. Favorite food and drink with cattle to build a side, capacity of 1.
Add a source point and a meeting point. The edge capacity of the source and food, drinks and sinks is 1, indicating that there is only one for each food and drink.
This is the biggest flow problem.
Sample Input
4 3 32 2 1 2 3 1//1th cattle like 2 kinds of food () 2 kinds of beverages (3,1) 2 2 2 3 1 22 2 1 3 1 22 1 1 3 3
Sample Output
3
1 /*2 POJ 3281 Maximum Flow3 //Source point-->food--> Ox (left)--ox (right)-->drink--> meeting point4 ///The essence is here, the cow is broken, to make sure a cow will choose a food and drink match5 6 */7 8#include <stdio.h>9#include <iostream>Ten#include <string.h> One#include <algorithm> A#include <queue> - using namespacestd; - the //**************************************************** - //Maximum Flow template - //initialization: G[][],start,end - //****************************************************** + Const intmaxn= -; - Const intinf=0x3fffffff; + intG[MAXN][MAXN];//capacity of the storage edge, no edge initialized to 0 A intPath[maxn],flow[maxn],start,end; at intN//The number of points, number 0-N.N, includes the source point and the sink point. - -queue<int>Q; - intBFS () - { - inti,t; in while(!q.empty ()) Q.pop ();//put the empty queue -memset (path,-1,sizeof(path));//initialize the path to 1 before each search topath[start]=0; +Flow[start]=inf;//The source point can have infinite stream flow into - Q.push (start); the while(!q.empty ()) * { $t=Q.front ();Panax Notoginseng Q.pop (); - if(T==end) Break; the //enumerate all the points, if the numbering start point of the point changes can be changed here + for(i=0; i<=n;i++) A { the if(i!=start&&path[i]==-1&&G[t][i]) + { -Flow[i]=flow[t]<g[t][i]?Flow[t]:g[t][i]; $ Q.push (i); $path[i]=T; - } - } the } - if(path[end]==-1)return-1;//I can't find the meeting point up there. I can't find the augmented path.Wuyi returnFlow[end]; the } - intEdmonds_karp () Wu { - intmax_flow=0; About intStep,now,pre; $ while((Step=bfs ())!=-1) - { -max_flow+=Step; -now=end; A while(now!=start) + { thePre=Path[now]; -g[pre][now]-=Step; $g[now][pre]+=Step; thenow=Pre; the } the } the returnMax_flow; - } in intMain () the { the intn,f,d; About while(SCANF ("%d%d%d", &n,&f,&d)! =EOF) the { theMemset (G,0,sizeof(g)); theN=f+d+2*n+1; +start=0; -End=N; the for(intI=1; i<=f;i++) g[0][i]=1;Bayi for(inti=f+2*n+1; i<=f+2*n+d;i++) g[i][n]=1; the for(intI=1; i<=n;i++) g[f+2*i-1][f+2*i]=1; the intK1,k2; - intu; - for(intI=1; i<=n;i++) the { thescanf"%d%d",&k1,&K2); the while(k1--) the { -scanf"%d",&u); theg[u][f+2*i-1]=1; the } the while(k2--)94 { thescanf"%d",&u); theg[f+2*i][f+2*n+u]=1; the }98 } Aboutprintf"%d\n", Edmonds_karp ()); - }101 return 0;102}
POJ 3281 Max Stream + build map