Test instructions: There are f vegetables, d drinks, each cow has a favorite dishes and drinks, each vegetable can only be selected once, drinks, ask how many cows can enjoy their favorite drinks and dishes
Analysis: When building a side, the cow is split into two points, out and into
1, source point to 1 per vegetable flow
2, each vegetable with all like this dish of cattle into point, flow 1
3, the in and out points of each cow, flow rate is 1
4, each cow's out point even all its favorite drinks, flow rate is 1
5, each beverage connection point, flow rate of 1
Then the maximum flow is the answer, this problem must be broken down, because a cow only eat once
Note: The template is based on the template of Lrj Great White Book
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<string>#include<stack>#include<vector>#include<map>#include<queue>#include<algorithm>#include<utility>using namespaceStd;typedefLong LongLL;Const intmaxn=4e2+5;Const intinf=0x3f3f3f3f;structedge{int from, To,cap,flow; Edge (intUintVintCintD): from(U), to (v), Cap (c), flow (d) {}};structdinic{ints,t; Vector<Edge>edges; Vector<int>G[MAXN]; intD[MAXN]; intCUR[MAXN]; BOOLVIS[MAXN]; voidinit () {edges.clear (); for(intI=0; i<maxn;++i) g[i].clear (); } BOOLBFs () {memset (Vis,0,sizeof(VIS)); Queue<int>P; Q.push (s); D[s]=0; Vis[s]=1; while(!Q.empty ()) { intx=Q.front (); Q.pop (); for(intI=0; I<g[x].size (); i++) {Edge&e=Edges[g[x][i]]; if(!vis[e.to]&&e.cap>e.flow) {vis[e.to]=1; D[e.to]=d[x]+1; Q.push (e.to); } } } returnVis[t]; } intDfsintXinta) {if(x==t| | a==0)returnA; intflow=0, F; for(int&i=cur[x]; I<g[x].size (); i++) {Edge&e=Edges[g[x][i]]; if(d[x]+1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-E.flow))) {E.flow+=F; Edges[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(a==0) Break; } } returnflow; } intMaxflow (intSintt) { This->s=s; This->t=T; intflow=0; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S,inf); } returnflow; } voidAddedge (intUintVintc) {Edge x (u,v,c,0), Y (V,u,0,0); Edges.push_back (x); Edges.push_back (y); intL=edges.size (); G[u].push_back (L-2); G[v].push_back (L-1); }}solve;intMain () {intn,f,d; while(~SCANF ("%d%d%d",&n,&f,&d)) {Solve.init (); for(intI=1; i<=n;++i) { intK1,k2; scanf ("%d%d",&k1,&K2); for(intj=0; j<k1;++j) { intU;SCANF ("%d",&T); Solve.addedge (U,i+f,1); } for(intj=0; j<k2;++j) { intV;SCANF ("%d",&v); Solve.addedge (n+f+d+i,v+n+f,1); } } ints=0, t=2*n+f+d+1; for(intI=1; i<=n;++i) Solve.addedge (I+f,i+n+f+d,1); for(intI=1; i<=f;++i) Solve.addedge (S,i,1); for(inti=n+f+1; i<=n+f+d;++i) Solve.addedge (I,t,1); printf ("%d\n", Solve.maxflow (s,t));} return 0;}View Code
POJ3281 Dining Maximum Flow