poj3281 Dining(最大流)

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11272 Accepted: 5180

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.

题意:n头牛,f种食物,d种饮料,每头牛对应若干食物和饮料,要给每一头牛分配一种饮料喝一种食物,互不重复。求最多几头牛可以分配到。

建图,边的权值均为1。源点向食物建边,饮料向汇点建边,每个牛建两个结点,两个结点间建边,以限制每头牛只用一次,再食物向其中一个牛的结点建边,牛的另一个结点向饮料建边。建图完毕就是简单的最大流。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 405

using namespace std;

int n, f, d;
int maps[maxn][maxn], q[maxn * maxn];
int dd[maxn];//记录每个点所在的层

int BFS()//BFS分层,判断是否存在增广路
{
    memset(dd, -1, sizeof(dd));
    dd[0] = 0;
    int qf, qr;
    qf = 1;
    qr = 1;
    q[qr++] = 0;
    while(qf < qr)
    {
        int x = q[qf++];
        for(int i = 0; i <= f + n * 2 + d + 1; i++)
        {
            if(dd[i] < 0 && maps[x][i] > 0)
            {
                dd[i] = dd[x] + 1;
                q[qr++] = i;
            }
        }
    }
    if(dd[f + n * 2 + d + 1] > 0)
        return 1;
    else
        return 0;
}

int Find(int x, int v)//找出某一条增广路的最大流
{
    int a;
    if(x == f + n * 2 + d + 1)
        return v;
    for(int i = 0; i <= f + n * 2 + d + 1; i++)
    {
        if(maps[x][i] > 0 && dd[i] == dd[x] + 1 && (a = Find(i, min(v, maps[x][i]))))//x到i有流量 且 i是x的下一层 且 i到汇点存在最大流
        {
            maps[x][i] -= a;//增广路
            maps[i][x] += a;//回退边
            return a;
        }
    }
    return 0;
}

int main()
{
    int p, t, x, y;
    while(scanf("%d%d%d", &n, &f, &d) != EOF)
    {
        memset(maps, 0, sizeof(maps));
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d%d", &x, &y);
            for(int j = 1; j <= x; j++)
            {
                scanf("%d", &p);
                maps[p][i + f] = 1;
            }
            for(int j = 1; j <= y; j++)
            {
                scanf("%d", &p);
                maps[i + f + n][f + n * 2 + p] = 1;
            }
        }
        for(int i = 1; i <= f; i++)
            maps[0][i] = 1;
        for(int i = f + 1; i <= f + n; i++)
            maps[i][i + n] = 1;
        for(int i = f + n * 2 + 1; i <= f + n * 2 + d; i++)
            maps[i][f + n * 2 + d + 1] = 1;
        int ans = 0;
        t = 0;
        while(BFS())//BFS搜索判断是否有从源点到汇点的通路
        {
            while(t = Find(0, 2000000))//查找增广路,求其最大流
                ans += t;
        }
        printf("%d\n", ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值