POJ 3281 Dining maximum flow + split point

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26007   Accepted: 11411

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 many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is 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 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:  NF, and  D 
Lines 2.. N+1: Each line  i starts with a two 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 will eat, and the  Di integers following that denote the drinks that cow  i will 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 wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 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.
 
Meaning of the questions: The first line of input N, F, D, N expressed personal, F kinds of food, D kinds of drinks
A second line starting from the N lines of the first two numbers represent the number d_num f_num and quantity of food and beverages, and the number f_num d_num behind a number of people are willing to choose which of several represent specific food and drink
Each food and drink is only one part, asked up to meet the wishes of several people at the same time
 
 
answer:
Network flow problem the key is to build the map, such as source -> Food -> People -> Beverages -> sink, but it may also have to pay attention to a person who like to get more than food and drink, so it is a map split in source  -> food -> people -> people -> beverages -> t , where people -> who is the same person to the same person, capacity is set to 1 on it.
 
Note: This question is relatively small amount of data (water), where EK algorithm is AC. Although hdu4292 Like this topic and ideas, but with EK algorithm is not over, TLE advance warning
 
#include <the iostream> 
#include <stdio.h> 
#include < String .h> 
#include < String > 
#include <algorithm> 
#include <Queue>
 the using  namespace STD;
 const  int MAXN = 500 ;
 const  int INF = 0x3FFFFFFF ;
 int CAP [MAXN] [MAXN]; // save the capacity of the edge, no edge is initialized to 0 
int path [MAXN], Flow [MAXN];
 int n-; // number of points, numbers [0, n], including the source and sink. 
int m, FF, D, ST, ENDD; 
Queue <int > P; // queue is placed outside time 262ms, 322ms after the inside into the function to, but not 
int BFS () 
{ 
    the while (! p.empty ()) 
        p.pop (); 
    Memset (path, - . 1 , the sizeof (path)); // every time before the route search is initialized to -1 
    path [ST] = 0 ; 
    flow [ST] = INF; // source node may have infinite stream flows into 
    p.push (st );
     the while (! ) p.empty () 
    { 
        int now = p.front (); 
        p.pop (); 
        IF (now == ENDD)
             BREAK ; 
        
        for (int I = 0 ; I <= n-; I ++) // enumerate all points, numbered starting point if there is a change can be changed here 
        {
             IF (ST && path I = [I] == -! . 1 && CAP [ now] [I]) 
            { 
                Flow [I] = Flow [now] <CAP [now] [I]? Flow [now]: CAP [now] [I]; 
                p.push (I); 
                path [I] = now; 
            } 
        } 
    } 
    IF (path [ENDD] == - . 1 ) // i.e. not found up the sink. Augmenting path can not find a 
        return - . 1 ;
     return Flow [ENDD]; 
} 
intEdmonds_Karp () 
{ 
    int mx_flow = 0 ;
     int STEP, POS, pre;
     the while ((BFS = STEP ()) = -! . 1 ) // STEP is the remaining amount 
    { 
        mx_flow + = STEP; 
        POS = ENDD; // from the sink update traffic point on 
        the while (POS =! ST) 
        { 
            pre = path [POS]; 
            CAP [pre] [POS] - = STEP; 
            CAP [POS] [pre] + = STEP; 
            POS = pre; 
        } 
    }
    return mx_flow;
}
int main()
{
    int food, drink, f_num, d_num;
    while (~scanf("%d%d%d", &m, &ff, &d))
    {
        memset(cap, 0, sizeof(cap));
        n = m * 2 + d + ff + 1;
        st = 0;
        endd = n;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d", &f_num, &d_num);
            for (int j = 0; j < f_num; j++)
            {
                scanf("%d", &food);
                cap[food][i + ff] = 1;
            }

            for (int j = 0; j < d_num; j++)
            {
                scanf("%d", &drink);
                cap[m + ff + i][drink + 2 * m + ff] = 1; 
            } 
        } 

        For ( int I = . 1 ; I <= FF; I ++) // for each food and drink only one 
            CAP [ 0 ] [I] = . 1 ; 

        for ( int I = . 1 ; I <= D; I ++ ) 
            CAP [FF + 2 * m + I] [ENDD] = . 1 ; 

        for ( int I = . 1 ; I <= m; I ++ ) 
            CAP [FF + I] [FF + m + I] = . 1 ; 

        the printf ( " D% \ n- "  , Edmonds_Karp ());
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11335126.html