POJ-3281 Dining Dinic最大流

来源:互联网 发布:js 创建key value数组 编辑:程序博客网 时间:2024/05/29 11:53

MMP

Dining
Time Limit: 2000MS Memory Limit: 65536K
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: N, F, 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.
Source

USACO 2007 Open Gold

劳资调了一下午。。。。

傻逼错误

就是一个简单的建图, 拆牛。。。

我的Dinic没有错!!!

# include <queue># include <cctype># include <cstdio># include <cstring># define N 100005inline void readIn ( int& x )  {    register char c ;    while ( ! isdigit ( c = getchar ( ) ) ) ;    for ( x = -48 + c ; isdigit ( c = getchar ( ) ) ; x = x * 10 + c - 48 ) ;}template < class T > inline T min ( T a, T b )  {   return a > b ? b : a ;  }class Network  {private :    struct edge  {        int to, w, nxt ;        edge ( ) {      }        edge ( int to, int w, int nxt ) : to ( to ), w ( w ), nxt ( nxt ) {     }      } g [N << 1] ;    int head [N], cur [N], ecnt ;    int S, T , dep [N] ;    inline int Dfs ( int u, int a )  {        if ( u == T || ! a )  return a ;        int flow = 0, v, f ;        for ( int& i = cur [u] ; i ; i = g [i].nxt )  {            v = g [i].to ;            if ( dep [v] == dep [u] + 1 )  {                f = Dfs ( v, min ( g [i].w, a - flow ) ) ;                g [i].w -= f, g [i ^ 1].w += f ;                flow += f ;                if ( a == flow )  return a ;            }        }        if ( ! flow )  dep [u] = -1 ;        return flow ;    }    inline bool Bfs ( int S, int T )  {        static std :: queue < int > q ;        memset ( dep, 0, sizeof ( int ) * ( T + 1 ) ) ;        dep [S] = 1 ;        q.push ( S ) ;        while ( ! q.empty ( ) )  {            int u = q.front ( ) ;  q.pop ( ) ;            for ( int i = head [u] ; i ; i = g [i].nxt )  {                int v = g [i].to ;                if ( g [i].w &&  ! dep [v] )  {                    dep [v] = dep [u] + 1 ;                    q.push ( v ) ;                }            }        }        return dep [T] ;    }public :    Network ( )  {  ecnt = 1 ; }    inline void Add_edge ( int u, int v, int w )  {        g [++ ecnt] = edge ( v, w, head [u] ) ;     head [u] = ecnt ;        g [++ ecnt] = edge ( u, 0, head [v] ) ;     head [v] = ecnt ;    }    inline int Dinic ( int S, int T )  {        this -> S = S, this -> T = T ;        int rt = 0 ;        while ( Bfs ( S, T ) )  {            memcpy ( cur, head, sizeof ( int ) * ( T + 1 ) ) ;             rt += Dfs ( S, 0x3f3f3f3f ) ;        }        return rt ;    }} Lazer ;int n, f, d ;int main ( )  {    readIn ( n ) ; readIn ( f ) ; readIn ( d ) ;    const int nn = n << 1 ;    const int S = 0, T = nn + f + d + 1 ;    for ( int i = 1 ; i <= n ; ++ i )  {        static int fi, di ;        readIn ( fi ) ; readIn ( di ) ;        while ( fi -- )  {            static int x ;            readIn ( x ) ;            Lazer.Add_edge ( nn + x, i, 1 ) ;        }        Lazer.Add_edge ( i, i + n, 1 ) ;        while ( di -- )  {            static int x ;            readIn ( x ) ;            Lazer.Add_edge ( i + n, nn + f + x, 1 ) ;        }    }    for ( int i = 1 ; i <= f ; ++ i )   Lazer.Add_edge ( S, nn + i, 1 ) ;    for ( int i = 1 ; i <= d ; ++ i )   Lazer.Add_edge ( nn + f + i, T, 1 ) ;    return printf ( "%d\n", Lazer.Dinic ( S, T ) ), 0 ;}