POJ 3281 Dining【网络流-拆点建图】

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


题意:

有F 种食物和D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用种食物和一种饮料。现在有N 头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。 (1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100 )

拆点建图:把牛拆成头和尾,两个点,中间容量为1,源点连接每个食物,容量为1,然后根据题意食物连接牛的头,容量inf,牛尾连接饮料,容量inf,每个饮料连接汇点,容量1

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
const int MAX = 666666;
const int Ni = 555;
struct Edge {
    int u, v, c;
    int next;
}edge[20 * Ni];
int n, m;
int edn;//边数    
int p[Ni];//父亲    
int d[Ni];
int sp, tp;//原点,汇点   
void addedge(int u, int v, int c)
{
    edge[edn].u = u; edge[edn].v = v; edge[edn].c = c;
    edge[edn].next = p[u]; p[u] = edn++;

    edge[edn].u = v; edge[edn].v = u; edge[edn].c = 0;
    edge[edn].next = p[v]; p[v] = edn++;
}
int bfs()
{
    queue <int> q;
    memset(d, -1, sizeof(d));
    d[sp] = 0;
    q.push(sp);
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        for (int i = p[cur]; i != -1; i = edge[i].next)
        {
            int u = edge[i].v;
            if (d[u] == -1 && edge[i].c > 0)
            {
                d[u] = d[cur] + 1;
                q.push(u);
            }
        }
    }
    return d[tp] != -1;
}
int dfs(int a, int b)
{
    int r = 0;
    if (a == tp)return b;
    for (int i = p[a]; i != -1 && r < b; i = edge[i].next)
    {
        int u = edge[i].v;
        if (edge[i].c > 0 && d[u] == d[a] + 1)
        {
            int x = min(edge[i].c, b - r);
            x = dfs(u, x);
            r += x;
            edge[i].c -= x;
            edge[i ^ 1].c += x;
        }
    }
    if (!r)d[a] = -2;
    return r;
}
int dinic(int sp, int tp)
{
    int total = 0, t;
    while (bfs())
    {
        while (t = dfs(sp, MAX))
            total += t;
    }
    return total;
}
int main()
{
    int f, d, temp;
    edn = 0;
    memset(p, -1, sizeof(p));    //初始化
    scanf("%d%d%d", &n, &f, &d);
    sp = 0; tp = 444;
    for (int i = 1; i <= f; i++)
    {
        addedge(sp, i, 1);

    }
    for (int i = 1; i <= d; i++)
        addedge(i + 111, tp, 1);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &f, &d);
        addedge(i + 222, i + 333, 1);
        for (int j = 1; j <= f; j++)
        {
            scanf("%d", &temp);
            addedge(temp, i + 222, 1);//food不加,牛加222  333  drink加111            
        }
        for (int j = 1; j <= d; j++)
        {
            scanf("%d", &temp);
            addedge(i + 333, temp + 111, 1);
        }
    }
    printf("%d\n", dinic(sp, tp));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinmingyi1998/article/details/81240583