Language:DefaultDining
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:9631 |
|
Accepted:4446 |
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as your cows as possible. Farmer John has cookedF(1 ≤F≤ 100) types of foods and preparedD(1 ≤D≤ 100) Types of drinks. Each of hisN(1 ≤NLess than or equal to 100) cows has decided whether she is willing to eat a particle food or drink a particle drink. farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both. Each dish or drink can only be consumed by one cow (I. e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2 ). Input Line 1: three space-separated integers:N,F, AndD Lines 2 ..N+ 1: Each lineIStarts with a two integersFiAndDi, The number of dishes that cowILikes and the number of drinks that cowILikes. The nextFiIntegers denote the dishes that cowIWill eat, andDiIntegers following that denote the drinks that cowIWill drink.Output Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishesSample Input 4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3 Sample output 3 Hint One way to satisfy three cows is: Cow 1: No meal Cow 2: Food #2, drink #2 Cow 3: Food #1, drink #1 Cow 4: Food #3, drink #3 The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.Source Usaco 2007 Open gold |
First, s connects to food, and the beverage connects to T, with the capacity equal to 1 (only one portion of each food)
Then the corresponding food is directed to the ox, and then connected to the corresponding edge of the beverage. The capacity is 1, indicating one method.
However, a cow can only take one copy, so the capacity of the node itself is equal to 1, so it is split.
# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <cmath> # include <cctype> # include <ctime> using namespace STD; # define for (I, n) for (INT I = 1; I <= N; I ++) # define fork (I, k, n) for (INT I = K; I <= N; I ++) # define rep (I, n) for (INT I = 0; I <n; I ++) # define Ford (I, n) for (INT I = N; I --) # define repd (I, n) for (INT I = N; I> = 0; I --) # define forp (x) for (INT P = pre [X]; P = Next [p]) # define forpiter (x) for (Int & P = ITER [X]; P = next [p]) # define lson (x <1) # define rson (x <1) + 1) # define MEM (a) memset (A, 0, sizeof (a); # define Memi () memset (A, 127, sizeof (a); # define Memi (a) memset (A, 128, sizeof (a); # define Inf (2139062143) # define F (100000007) # define maxn (100 + 10) # define maxf (100 + 10) # define maxd (100 + 10) # define maxn (1000 + 10) # define maxm (30300) * 2 + 100) long MUL (long A, long B) {return (A * B) % F;} long add (long a, long B) {return (a + B) % F ;} long long sub (long a, long B) {return (a-B + (a-B)/f * F + F) % F;} typedef long ll; class max_flow // dinic + current arc optimization {public: int N, S, T; int Q [10000]; int edge [maxm], next [maxm], pre [maxn], weight [maxm], size; void addedge (int u, int V, int W) {edge [++ size] = V; weight [size] = W; next [size] = pre [u]; Pre [u] = size;} void ad Dedge2 (int u, int V, int W) {addedge (U, V, W), addedge (v, U, 0);} bool B [maxn]; int d [maxn]; bool spfa (int s, int t) {for (I, n) d [I] = inf; MEM (B) d [Q [1] = s] = 0; B [s] = 1; int head = 1, tail = 1; while (Head <= tail) {int now = Q [head ++]; forp (now) {Int & V = edge [p]; If (weight [p] &! B [v]) {d [v] = d [now] + 1; B [v] = 1, Q [++ tail] = V ;}}} return B [T];} int ITER [maxn]; int DFS (INT X, int f) {If (x = T) return F; forpiter (X) {int v = edge [p]; If (weight [p] & D [x] <D [v]) {int nowflow = DFS (V, min (weight [p], F); If (nowflow) {weight [p]-= nowflow; weight [P ^ 1] + = nowflow; return nowflow ;}}} return 0;} int max_flow (int s, int t) {int flow = 0; while (spfa (S, T) {for (I, n) ITER [I] = pre [I]; int F; while (F = DFS (S, INF) flow + = f;} return flow;} void MEM (INT N, int S, int t) {(* This ). N = N; (* This ). T = T; (* This ). S = s; size = 1; MEM (pre)} s; int N, F, D; int main () {// freopen ("poj3281.in", "r ", stdin); // freopen (". out "," W ", stdout); CIN> N> F> D; int S = 1, t = 2 + 2 * n + F + D; S. mem (T, 1, t); for (I, f) s. addedge2 (s, 1 + I, 1); For (I, d) s. addedge2 (1 + F + 2 * n + I, T, 1); For (I, n) {S. addedge2 (1 + F + I, 1 + F + N + I, 1); int Fi, Di, P; scanf ("% d", & Fi, & di); For (J, FI) {scanf ("% d", & P); S. addedge2 (1 + P, 1 + F + I, 1) ;}for (J, DI) {scanf ("% d", & P); S. addedge2 (1 + F + N + I, 1 + F + 2 * n + p, 1) ;}} cout <S. max_flow (S, T) <Endl; return 0 ;}
Poj 3281 (dining-network stream splitting point) [template: network stream dinic]