poj3281 Dining 題解

傳送門

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 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.
題意:農夫爲他的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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章