Title Link: http://poj.org/problem?id=3281
Test instructions
Give the cattle n, drink D and the number of food f, each cow gives a favorite drink and food, and finally find out the number of cows to meet
Solution: Source point to food construction side, from food to New Jian side, cow to drink build side, drink to meeting point building edge, seek maximum flow. The cow wants to break the control flow to 1.
All of them are forward sides, and the weights are all 1.
There's a 2*n+f+d+2 vertex.
0 represents the source point, 2*n+f+d+1 represents the meeting point
1 to F for food points, f+1 to f+2*n for cattle points, f+2*n+1 to f+2*n+d for beverage points
Code:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;Const intMAXN =1010;maximum value of//pointsConst intMAXM =400010;maximum of//number of sidesConst intINF =0x3f3f3f3f;structedge{intTo, next, cap, flow;} EDGE[MAXM];//Note is MAXMintTolintHEAD[MAXN];intGAP[MAXN], DEP[MAXN], PRE[MAXN], CUR[MAXN];voidInit () {tol =0;memset(Head,-1,sizeof(head));}//plus side, unidirectional figure three parameters, bidirectional figure four parametersvoidAddedge (intUintVintWintRW =0) {edge[tol].to = v; edge[tol].cap = w; edge[tol].next = Head[u]; Edge[tol].flow =0; Head[u] = tol++; edge[tol].to = u; Edge[tol].cap = RW; Edge[tol].next = Head[v]; Edge[tol].flow =0; HEAD[V] = tol++;}//Input parameters: Total number of start, end, pointthe number of points is not affected, as long as the total number of input pointsintSapintStartintEndintN) {memset(Gap,0,sizeof(GAP));memset(DEP,0,sizeof(DEP));memcpy(Cur, head,sizeof(head));intu = start; Pre[u] =-1; gap[0] = N;intAns =0; while(Dep[start] < N) {if(U = = end) {intMin = INF; for(inti = Pre[u]; I! =-1; i = pre[edge[i ^1].to])if(Min > Edge[i].cap-edge[i].flow) Min = Edge[i].cap-edge[i].flow; for(inti = Pre[u]; I! =-1; i = pre[edge[i ^1].to]) {edge[i].flow + = Min; Edge[i ^1].flow = Min; } u = start; Ans + = Min;Continue; }BOOLFlag =false;intV for(inti = Cur[u]; I! =-1; i = edge[i].next) {v = edge[i].to;if(Edge[i].cap-edge[i].flow && Dep[v] +1= = Dep[u]) {flag =true; Cur[u] = pre[v] = i; Break; } }if(flag) {u = V;Continue; }intMin = N; for(inti = Head[u]; I! =-1; i = edge[i].next)if(Edge[i].cap-edge[i].flow && dep[edge[i].to] < Min) {Min = dep[edge[i].to]; Cur[u] = i; } gap[dep[u]]--;if(!gap[dep[u]])returnAns Dep[u] = Min +1; gap[dep[u]]++;if(U! = start) u = edge[pre[u] ^1].to; }returnAns;}intM, N, F, D, X, y;intA, B, C;intMain () { while(scanf("%d%d%d", &n, &f, &d)! = EOF) {init ();intTMP = f +2*n;intso =0;//Source Point intto = f + N *2+ D +1;//Meeting Point inttot = f + N *2+ D +2; for(inti =1; I <= F; i++)//Source point point to foodAddedge (So, I,1); for(inti =1; I <= D; i++)//Drinks point to Meeting pointAddedge (tmp + I, to,1); for(inti =1; I <= N; i++)//Ox Pointing cowAddedge (f + i, F + n + I,1); for(inti =1; I <= n;i++) {scanf("%d%d", &x,&y); while(x--) {scanf("%d", &m); Addedge (M, F + i,1); } while(y--) {scanf("%d", &m); Addedge (f + n + I,2*n + F + M,1); } }intAns = SAP (so,to, TOT);printf("%d\n", ans); }return 0;}
Copyright NOTICE: Reprint please indicate the source.
POJ 3281 Dining "Maximum Flow"