poj3281 Dining 题解

来源:互联网 发布:mac终端进入指定目录 编辑:程序博客网 时间:2024/05/22 10:10

传送门

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 Fiintegers 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 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.
题意:农夫为他的N头牛准备了F种食物和D中饮料,每头牛都有自己喜欢的食物和饮料,而每种食物和饮料只能分配给一头牛,最多能有多少头牛得到自己喜欢的食物和饮料。

这道题就是一个最大流的变体,将牛分成两边,一边和食物匹配,一边和饮料匹配。然后相同的牛之间连一条边,再增加一个超级原点s和超级汇点t即可。


如上图所示建图,然后套用网络流模板即可。


#include<stdio.h>#include<iostream>#include<set>#include<queue>#include<algorithm>#include<math.h>#include<string.h>#include<string>#include<vector>using namespace std;const int inf = 0x3f3f3f3f ;const int maxn = 505 ;struct edge{    int to , cap , rev ;};vector<edge> G[maxn];bool used[maxn];void add_edge(int from , int to , int cap){    edge e1 , e2 ;    e1.to = to;    e1.cap = cap ;    e1.rev = G[to].size();    G[from].push_back(e1);    e2.to = from;    e2.cap = 0 ;    e2.rev = G[from].size() - 1;    G[to].push_back(e2);}int dfs(int v , int t , int f){    if(v == t)        return f ;    used[v] = true;    for(int i = 0 ; i <G[v].size() ; i ++){        edge &e = G[v][i] ;        if(!used[e.to] && e.cap > 0){            int d = dfs(e.to , t , min(f , e.cap));            if(d > 0){                e.cap -= d;                G[e.to][e.rev].cap += d;                return d;            }        }    }    return 0;}int max_flow(int s , int t){    int flow = 0 ;    for( ; ; ){        memset(used , false , sizeof(used));        int f = dfs(s , t , inf);        if(f == 0 )            return flow ;        flow += f ;    }}//最大流模板结束int n , f , d ;int main(){    while(~scanf("%d%d%d",&n,&f,&d)){        int s = 0 , t = 2*n + f + d + 1;        int i1 ,i2 ;        for(int i = 0 ; i <= t ; i ++)            G[i].clear();//一系列的建图        for(int i = 1 ; i <= f ; i ++)            add_edge(0 , i  , 1) ;        for(int i = f+2*n+1 ; i <= f+2*n+d ; i ++)            add_edge(i , t , 1) ;        for(int i = f+1 ; i <= f+n ; i ++){            add_edge(i , i + n , 1) ;        }        for(int i = 1 ; i <= n ; i ++){            int x;            int fi , di ;            scanf("%d %d",&fi , &di);            for(int i1 = 0 ; i1 < fi ; i1++){                scanf("%d" , &x) ;                add_edge(x , i + f , 1 );            }            for(int i2 = 0 ; i2 < di ; i2 ++){                scanf("%d" , &x);                add_edge(f+n+i , f+2*n+x , 1);            }        }        printf("%d\n" ,max_flow(s , t));    }    return 0;}