Question link: http://poj.org/problem? Id = 3281
I personally think that the difficulty of this question lies in the figure below. It should have multiple source points and starting points, so we can set a super source point and super sink point.
Divide the ox into 2, one ox eats food, one ox eats grass, two cows correspond one by one, and the capacity is 1
Therefore, the total number of nodes is 2 * n + F + D + 2.
# Include <iostream> # include <cstdio> # include <queue> # include <vector> # include <cstring> # define maxint int_maxusing namespace STD; const int max_n = 500; const int INF = 100000000; Class edge {public: int flow; // traffic int cap; // capacity edge (INT flow, int cap): flow (flow ), CAP (CAP) {}edge () {flow = 0; Cap = 0 ;}}; edge maps [max_n] [max_n]; int dis [max_n]; // It is used to divide data into int N, F, D, totaln; int S, T; // The parameter is the source point and the sink point bool BFS (int s, int t ){ Int V, I; queue <int> que; memset (DIS, 0, sizeof (DIS); Que. Push (s); DIS [s] = 1; while (! Que. empty () {v = que. front (); que. pop (); For (INT I = 1; I <= totaln; I ++) {If (! Dis [I] & maps [v] [I]. cap> maps [v] [I]. flow) {que. push (I); // The traffic at this point can still be increased by DIS [I] = dis [v] + 1 ;}// reach the end if (V = T) return true;} return false;} int DFS (int s, int T, int f) {If (S = T) return F; int temp = f; int D; for (INT I = 1; I <= totaln; I ++) {If (DIS [I] = (DIS [s] + 1) & temp & maps [s] [I]. cap> maps [s] [I]. flow) {d = DFS (I, T, min (temp, maps [s] [I]. cap-maps [s] [I]. flow); // cout <D <Endl; maps [s] [I]. flow + = D; Maps [I] [s]. flow-= D; temp-= D;} return F-temp;} int dinic (int s, int t) {int maxflow = 0; while (BFS (S, t) {// cout <maxflow <Endl; maxflow + = DFS (S, T, INF);} return maxflow;} // indicates the number of cows, number of dishes, number of soups void build_graph (int n, int F, int d) {totaln = 2 * n + F + D + 2; S = 2 * n + F + D + 1; // source point t = 2 * n + F + D + 2; // sink // cout <S <"" <t <Endl; For (INT I = 1; I <= f; I ++) {// The Source point points to the food maps [s] [I]. CAP = 1 ;}for (INT I = 1; I <= D; I ++) {// The beverage points to the sink Maps [2 * n + F + I] [T]. CAP = 1 ;}int lf, LD; // Number of favorite dishes and the number of hot dishes for (INT I = 1; I <= N; I ++) {// indicates the I-th ox. // the I-th ox of the first category points to the I-th ox of the second category, maps [F + I] [F + I + N]. CAP = 1; scanf ("% d", & lf, & LD); int lFF, LDD; // For (Int J = 1; j <= lf; j ++) {// s refers to the first class of scanf ("% d", & lFF); maps [lFF] [I + F]. CAP = 1 ;}for (Int J = 1; j <= LD; j ++) {// The second type of cattle points to drink scanf ("% d ", & LDD); maps [I + F + N] [n + F + N + LDD]. CAP = 1 ;}}int main () {// freopen ("Maximum stream _ poj_3281.t XT "," r ", stdin); While (scanf (" % d ", & N, & F, & D )! = EOF) {build_graph (n, F, d); printf ("% d \ n", dinic (S, T ));}}
Network stream_maximum stream_poj 3281