POJ3281(最大流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/creatorx/article/details/78147459
Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19600   Accepted: 8716

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.

Source


解题思路:将牛拆成两个点,食物流向左牛,左牛流向右牛,右牛流向饮料,超级源点流向所有食物,所有饮料流向超级汇点,然后求一个超级源点到超级汇点的最大流就行。

#include <stdio.h>
#include <queue>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
int N, F, D;
int s, t;
int tot;
int head[1000];
int level[1000];
bool visit[1000];
struct edge
{
    int v, last, w;
} Edge[300000];
void add(int u, int v, int w)
{
    Edge[tot].v = v;
    Edge[tot].w = w;
    Edge[tot].last = head[u];
    head[u] = tot++;
    Edge[tot].v = u;
    Edge[tot].w = 0;
    Edge[tot].last = head[v];
    head[v] = tot++;
}
void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
    s = 0;
    t = N + N + F + D + 1;
}
bool bfs()
{
    queue<int> q;
    while(!q.empty()) q.pop();
    memset(level, -1, sizeof(level));
    memset(visit, false, sizeof(visit));
    level[s] = 0;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        visit[u] = true;
        if(u == t) return true;
        for(int i = head[u]; i != -1; i = Edge[i].last)
        {
            int v = Edge[i].v;
            int w = Edge[i].w;
            if(!visit[v] && w)
            {
                visit[v] = true;
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}
int dfs(int ss, int tt, int ff)
{
    if(ss == tt) return ff;
    int res = 0;
    for(int i = head[ss]; i != -1; i = Edge[i].last)
    {
        int v = Edge[i].v;
        int w = Edge[i].w;
        int temp = min(w, ff - res);
        if(level[v] == level[ss] + 1 && temp)
        {
            int ans = dfs(v, tt, temp);
            Edge[i].w -= ans;
            Edge[i^1].w += ans;
            res += ans;
            if(res == ff) return res;
        }
    }
    return res;
}
int Dinic()
{
    int Maxflow = 0;
    while(bfs()) Maxflow += dfs(s, t, inf);
    return Maxflow;
}
int main()
{
    while(scanf("%d%d%d", &N, &F, &D) != EOF)
    {

        init();
        int fnum, dnum;
        //建图
        for(int i = 1; i <= F; i++)
        {
            add(s, i + 2 * N, 1);
        }
        for(int i = 1; i <= D; i++)
        {
            add(i + F + 2 * N, t, 1);
        }
        for(int i = 1; i <= N; i++)
        {
            add(i, i + N, 1);
            scanf("%d%d", &fnum, &dnum);
            int value;
            for(int j = 1; j <= fnum; j++)
            {
                scanf("%d", &value);
                add(N * 2 + value, i, 1);
            }
            for(int j = 1; j <= dnum; j++)
            {
                scanf("%d", &value);
                add(i + N, value + F + 2 * N, 1);
            }

        }
        printf("%d\n", Dinic());
    }
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页