POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

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 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.

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

建完图就是裸的最大流,这里用Edmonds-Karp算法求取

#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));
}
时间: 2024-08-06 20:07:14

POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)的相关文章

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,

poj 3281 Dining(最大流)

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 prefer

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

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

POJ 3281 Dining (网络流之最大流)

题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到喜欢的食物和饮料? 析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点, 一个和食物连,一个和饮料连,最后跑一遍最大流即可. 代码如下: #pragma comment(linker, "/STACK:

poj 3281 Dining 【最大流】

题目链接:http://poj.org/problem?id=3281 题意: 给出牛n,饮料d还有食物f的数量,每头牛给出喜欢的饮料和食物,最后求出能够满足的牛的数量 解法:源点到食物建边, 由食物到牛建边, 牛到饮料建边 ,饮料到汇点建边 ,求最大流.牛要拆点控制流量为1 全部是有向的边,而且权值全部为1 有2*n+f+d+2个顶点 0表示源点,2*n+f+d+1表示汇点 1到f为食物点,f+1到f+2*n为牛点,f+2*n+1到f+2*n+d为饮料点 代码: #include <stdio

POJ 3498【最大流+拆点建图】

题意: 在X,Y坐标系中有N(N<=100)个冰块...有些冰块上有若干只企鹅..每只企鹅一次最多跳M距离..一个冰块在有Mi个企鹅离开..就会消失..问有哪些冰块可以作为集合点..就是所有企鹅都能成功到这个冰块上来. 这个题建图比较有意思. 把每块冰分成两个点i和i+n. i表示进入i冰块的点(可以有无数企鹅过来,所以从别的冰到i有边,容量为INF) i+n表示从i冰块出去的点(最多只能有Mi企鹅从这跳出去,所以从i到i+n有边,且容量为Mi) 从源点S到i有边(S, i, i点初始企鹅数).

POJ 3281 Dining(最大流)

POJ 3281 Dining 题目链接 题意:n个牛,每个牛有一些喜欢的食物和饮料,每种食物饮料只有一个,问最大能匹配上多少只牛每个牛都能吃上喜欢的食物和喜欢的饮料 思路:最大流,建模源点到每个食物连一条边,容量为1,每个饮料向汇点连一条边容量为1,然后由于每个牛有容量1,所以把牛进行拆点,然后食物连向牛的入点,牛的出点连向食物,跑一下最大流即可 代码: #include <cstdio> #include <cstring> #include <queue> #in