B - Dining

B - Dining

POJ - 3281

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.

题目大意:

有一个厨师做了一些菜和饮料,但是有些牛只喜欢某些饮料和食物,但是为了能够分配给更多的牛,每个牛只能有一道自己喜欢的饮料和菜,问最多有几个牛能有饮料和菜?

题解:网络流最大流问题,建边为1到F--F到F+N为菜到牛的边,F到F+N---F+N到F+N×2为牛到牛的边,F+N到F+N*2---F+N*2+D为牛到饮料的边在最后面设两个点为源点和汇点,然后跑一遍最大流就出结果了。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

const int MAXN = 10100; 
const int MAXM = 10100;
const int INF = 0x3f3f3f3f;

struct Node
{
        int from, to, next;
        int cap;
} edge[MAXM];
int tol;
int head[MAXN];
int dep[MAXN];
int gap[MAXN];

int n;

void init()
{
        tol = 0;
        memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int w)
{
        edge[tol].from = u;
        edge[tol].to = v;
        edge[tol].cap = w;
        edge[tol].next = head[u];
        head[u] = tol++;
        edge[tol].from = v;
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
}
void BFS(int start, int endd)
{
        memset(dep, -1, sizeof(dep));
        memset(gap, 0, sizeof(gap));
        gap[0] = 1;
        int que[MAXN];
        int frontd, rear;
        frontd = rear = 0;
        dep[endd] = 0;
        que[rear++] = endd;
        while (frontd != rear)
        {
                int u = que[frontd++];
                if (frontd == MAXN)
                {
                        frontd = 0;
                }
                for (int i = head[u]; i != -1; i = edge[i].next)
                {
                        int v = edge[i].to;
                        if (dep[v] != -1)
                        {
                                continue;
                        }
                        que[rear++] = v;
                        if (rear == MAXN)
                        {
                                rear = 0;
                        }
                        dep[v] = dep[u] + 1;
                        ++gap[dep[v]];
                }
        }
}
int SAP(int start, int endd)
{
        int res = 0;
        BFS(start, endd);
        int cur[MAXN];
        int S[MAXN];
        int top = 0;
        memcpy(cur, head, sizeof(head));
        int u = start;
        int i;
        while (dep[start] < n)
        {
                if (u == endd)
                {
                        int temp = INF;
                        int inser;
                        for (i = 0; i < top; i++)
                                if (temp > edge[S[i]].cap)
                                {
                                        temp = edge[S[i]].cap;
                                        inser = i;
                                }
                        for (i = 0; i < top; i++)
                        {
                                edge[S[i]].cap -= temp;
                                edge[S[i] ^ 1].cap += temp;
                        }
                        res += temp;
                        top = inser;
                        u = edge[S[top]].from;
                }
                if (u != endd && gap[dep[u] - 1] == 0)
                {
                        break;
                }
                for (i = cur[u]; i != -1; i = edge[i].next)
                        if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1)
                        {
                                break;
                        }
                if (i != -1)
                {
                        cur[u] = i;
                        S[top++] = i;
                        u = edge[i].to;
                }
                else
                {
                        int mind = n;
                        for (i = head[u]; i != -1; i = edge[i].next)
                        {
                                if (edge[i].cap == 0)
                                {
                                        continue;
                                }
                                if (mind > dep[edge[i].to])
                                {
                                        mind = dep[edge[i].to];
                                        cur[u] = i;
                                }
                        }
                        --gap[dep[u]];
                        dep[u] = mind + 1;
                        ++gap[dep[u]];
                        if (u != start)
                        {
                                u = edge[S[--top]].from;
                        }
                }
        }
        return res;
}

int main()
{
    int N,D,F;
    int a,b;
    int f;
    while(~scanf("%d %d %d",&N,&F,&D))
    {
        init();
        for(int i=1;i<=F;i++)
        {
            addedge(F+N*2+D+1,i,1);
        }
        for(int i=1;i<=N;i++)
        {
            addedge(F+i,F+N+i,1);
        }
        for(int i=1;i<=N;i++)
        {
            scanf("%d %d",&a,&b);
            for(int j=1;j<=a;j++)
            {
                scanf("%d",&f);
                addedge(f,F+i,1);
            }
            for(int j=1;j<=b;j++)
            {
                scanf("%d",&f);
                addedge(F+N+i,F+N*2+f,1);
            }
        }
        for(int i=1;i<=D;i++)
        {
            addedge(F+N*2+i,F+N*2+D+2,1);
        }
        n=F+N*2+D+2;
        printf("%d\n",SAP(F+N*2+D+1,F+N*2+D+2));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The_city_of_the__sky/article/details/83279268