POJ 3281 Dining (网络流最大流 拆点建图 EK Dinic)

91 篇文章 1 订阅


Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10159 Accepted: 4676

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 preparedD (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 andDi, the number of dishes that cow i likes and the number of drinks that cowi likes. The next Fi integers denote the dishes that cowi will eat, and the Di integers following that denote the drinks that cowi 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

USACO 2007 Open Gold

题目链接:poj.org/problem?id=3281

题目大意:一个农夫有n头牛,f种食物,d种饮料各一份,第i头牛喜欢fi种食物(fi1, fi2...),di种饮料(di1, di2...),一头牛只能选择一份搭配(一种食物+一种饮料)求农夫最多可以同时满足多少头牛的喜好

题目分析:第一道拆点建图的题,觉得很有趣,按源点 -> Food -> 牛左 -> 牛右 -> Drink -> 汇点建图,图中牛左 -> 牛右的目的是保证一头牛只选一种食物,方法是给牛左到牛右的边赋值为1,即容量为1,总的定点个数为f + d + 2 * n + 1
第一阶段:源点 -> Food  =>  0 -> (1...f)
第二阶段:Food -> 牛左  =>  (1...f) -> (f + 1, f + n)
第三阶段:牛左 ->  牛右  =>  (f + 1, f + n) -> (f + n + 1, f + 2 * n)
第四阶段:牛右 ->  Drink =>  (f + n + 1, f + 2 * n) -> (f + 2 * n + 1, f + 2 * n + d)
第五阶段:Drink -> 汇点  =>  (f + 2 * n + 1,  f + 2 * n + d) -> f + 2 * n + d + 1
建完图就是裸的最大流

EK
400ms+
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int const INF = INT_MAX;
int const MAX = 405;
int c[MAX][MAX];
int f[MAX][MAX];
int a[MAX];
int pre[MAX];
int n, food, drink;

int Edmonds_Karp(int s, int t)
{
    int ans = 0;
    queue <int> q;
    while(true)
    {
        memset(a, 0, sizeof(a));
        a[s] = INF;
        q.push(s);
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            for(int v = s; v <= t; v++)
            {
                if(!a[v] && c[u][v] > f[u][v])
                {
                    a[v] =  min(a[u], c[u][v] - f[u][v]);
                    pre[v] = u;
                    q.push(v);
                }
            }
        }
        if(a[t] == 0)
            break;
        for(int u = t; u != s; u = pre[u])
        {
            f[pre[u]][u] += a[t];
            f[u][pre[u]] -= a[t];
        }
        ans += a[t];
    }
    return ans;
}

int main()
{
    memset(c, 0, sizeof(c));
    memset(f, 0, sizeof(f));
    scanf("%d %d %d", &n, &food, &drink);
    for(int i = 1; i <= food; i++)
        c[0][i] = 1;
    for(int i = 1; i <= n; i++)
    {
        int nf, nd, getf, getd;
        scanf("%d %d", &nf, &nd);
        while(nf --)
        {
            scanf("%d", &getf);
            c[getf][food + i] = 1;
        }
        c[food + i][food + n + i] = 1;
        while(nd --)
        {
            scanf("%d", &getd);
            c[food + n + i][food + 2 * n + getd] = 1;
        }
    }
    for(int i = 1; i <= drink; i++)
        c[food + 2 * n + i][food + 2 * n + drink + 1] = 1;
    printf("%d\n", Edmonds_Karp(0, food + 2 * n + drink + 1));
}



Dinic:47ms
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1000;
int cap[MAX][MAX];
int d[MAX];
int n, food, drink;
int sink, src;

int BFS()
{
    memset(d, 0, sizeof(d));
    queue <int> Q;
    d[src] = 0;
    Q.push(src);
    while(!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for(int i = src + 1; i <= sink; i++)
        {
            if(cap[cur][i] > 0 && d[i] == 0)
            {
                d[i] = d[cur] + 1;
                Q.push(i);
            }
        }
    }
    return d[sink];
}

int Dinic(int t, int flow)
{
    if(t == sink || flow == 0)
        return flow;
    int tmp = flow;
    for(int i = src; i <= sink; i++)
    {
        if(d[i] == d[t] + 1 && cap[t][i] > 0)
        {
            int mi = Dinic(i, min(flow, cap[t][i]));
            cap[t][i] -= mi;
            cap[i][t] += mi;
            flow -= mi;
        }
    }
    return tmp - flow;
}

int main()
{
    memset(cap, 0, sizeof(cap));
    scanf("%d %d %d", &n, &food, &drink);
    src = 0;
    sink = food + 2 * n + drink + 1;
    for(int i = 1; i <= food; i++)
        cap[src][i] = 1;
    for(int i = 1; i <= n; i++)
    {
        int nf, nd, getf, getd;
        scanf("%d %d", &nf, &nd);
        while(nf --)
        {
            scanf("%d", &getf);
            cap[getf][food + i] = 1;
        }
        cap[food + i][food + n + i] = 1;
        while(nd --)
        {
            scanf("%d", &getd);
            cap[food + n + i][food + 2 * n + getd] = 1;
        }
    }
    for(int i = 1; i <= drink; i++)
        cap[food + 2 * n + i][sink] = 1;
    int ans = 0;
    while(BFS())
        ans += Dinic(src, INF);
    printf("%d\n", ans);
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值