Topic Connection: http://poj.org/problem?id=1087
Test instructions
n Types of sockets, M appliances, Group F (x, y) indicate that socket x can replace the socket y, asking you to charge a few appliances.
Solution: The starting point to the socket building edge, capacity 1, electrical appliances to the meeting point building edge, capacity 1, socket to electrical building edge, capacity 1, can replace the socket between the building, capacity infinity. Then set the board ... To find the maximum flow.
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>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; Map<string, int>Hash;stringx, y;intMain () { while(Cin>> N) {init (); Hash.clear ();intNUM1 =2;intfrom =0;intEnd =1; for(inti =1; I <= N; i++) {Cin>> x; HASH[X] = NUM1; Addedge (0, NUM1,1); num1++; }Cin>> m; for(inti =1; I <= m; i++) {Cin>> x >> y;if(Hash[x] = =0) hash[x] = num1++;if(Hash[y] = =0) Hash[y] = num1++; Addedge (Hash[x], end,1); Addedge (Hash[y], hash[x],1); }Cin>> F; for(inti =1; I <= F; i++) {Cin>> x >> y;if(Hash[x] = =0) hash[x] = num1++;if(Hash[y] = =0) Hash[y] = num1++; Addedge (Hash[y], hash[x],10000000); }intAns = sap (from, end, NUM1);printf("%d\n", M-ans); }return 0;}
Copyright NOTICE: Reprint please indicate the source.
POJ 1087 A Plug for UNIX maximum flow