POJ 3281 Dining (network flow maximum flow resolution EDMONDS-KARP algorithm)

Source: Internet
Author: User


Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10159 Accepted: 4676

Description

Cows is such finicky eaters. Each cow have a preference for certain foods and drinks, and she'll consume no others.

Farmer John had cooked fabulous meals for him cows, but he forgot to check his menu against their preferences. Although he might not being able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows As possible.

Farmer John has cooked f (1≤ F ≤100) types of foods and preparedD (1≤ D ≤100) type S of drinks. Each of him N (1≤ n ≤100) cows has decided whether she's willing to eat a particular food or drink a Particular 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 are only being consumed by one cow (i.e., once food type 2 are assigned to a cow, no other cow can be assig Ned Food type 2).

Input

Line 1:three space-separated integers: N, F, and D
Lines 2.. N+1:each line i starts with a II integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i 'll eat, and the Di integers following That's denote the drinks that cow I 'll drink.

Output

Line 1: A single integer So is the maximum number of cows so can be fed both food and drink, conform to their Wishes

Sample 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-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 is only three kinds of food or drink. Other test data sets is more challenging, of course.

Source

Usaco Open Gold

Title Link: poj.org/problem?id=3281

The main idea: a farmer has n cows, f kinds of food, d drink each one, the first cow like fi seed food (fi1, Fi2 ...), di kind of beverage (DI1, Di2 ...), a cow can only choose a match (a food + a drink) to ask farmers to meet the maximum number of cows at the same time

Title analysis: The first demolition point of the problem, it is very interesting, according to the source point, food-------------------------------Drink, the left-to-right New That is, the capacity is 1, the total number of fixed points is F + D + 2 * n + 1
Phase I: Food 0, Source point, (1...F)
Phase II: Food, Ox Left (1...F), (f + 1, F + N)
Phase III: Ox left, ox right = (f + 1, F + N) (f + n + 1, f + 2 * N)
Stage four: Ox right-Drink = (f + n + 1, f + 2 * N), (f + 2 * n + 1, f + 2 * n + D)
Fifth stage: Drink-a meeting point (f + 2 * n + 1, f + 2 * n + D) f + 2 * n + D + 1
The graph is the bare maximum flow, which is obtained by edmonds-karp algorithm.


#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;  int const INF = int_max;int Const MAX = 405;int c[max][max];int f[max][max];int a[max];int pre[max];int N, Food, Drink;int    Edmonds_karp (int s, int t) {int ans = 0;    Queue <int> q;        while (true) {memset (A, 0, sizeof (a));        A[s] = INF;        Q.push (s);            while (!q.empty ()) {int u = q.front ();            Q.pop ();                    for (int v = s; v <= t; v++) {if (!a[v] && c[u][v] > F[u][v]) {                    A[v] = min (A[u], C[u][v]-f[u][v]);                    PRE[V] = u;                Q.push (v);        }}} if (a[t] = = 0) break;            for (int u = t; u! = s; u = Pre[u]) {F[pre[u]][u] + = a[t];        F[u][pre[u]]-= a[t];    } ans + = a[t]; } return ans; int main () {memset (c, 0, siZeof (c));    memset (f, 0, sizeof (f));    scanf ("%d%d%d", &n, &food, &drink);    for (int i = 1; I <= food; i++) c[0][i] = 1;        for (int i = 1; I <= n; i++) {int nf, ND, GETF, getd;        scanf ("%d%d", &AMP;NF, &nd);            while (NF-) {scanf ("%d", &AMP;GETF);        C[getf][food + i] = 1;        } C[food + I][food + n + i] = 1;            while (ND--) {scanf ("%d", &getd);        C[food + n + i][food + 2 * n + getd] = 1;    }} for (int i = 1; I <= drink; i++) C[food + 2 * n + i][food + 2 * n + drink + 1] = 1; printf ("%d\n", Edmonds_karp (0, Food + 2 * n + drink + 1));}


POJ 3281 Dining (network flow maximum flow resolution EDMONDS-KARP algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.