POJ 3281 Dining 最大流+拆点

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26007   Accepted: 11411

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.
 
题意:第一行输入N,F,D,表示有N个人,F种食物,D种饮料
从第二行开始的N行,每行前两个数f_num和d_num表示一个人愿意选择的食物数量和饮料数量,后面的f_num个数和d_num个数分别表示具体的哪几种食物和饮料
每种食物和饮料只有1份,问最多可以同时满足几个人的意愿
 
 
题解:
网络流题关键的就是建图了,例如 源点 -> 食物 -> 人 -> 饮料 -> 汇点,但是要注意这也可能会有一个人拿到多对喜欢的食物和饮料,所以要把图拆为 源点 -> 食物 -> 人 -> 人 -> 饮料 -> t,其中 人 -> 人是同一个人到同一个人,设定容量为1,就可以了。
 
注意:这道题数据量比较小(水),这里是用EK算法可以AC。虽然hdu4292与这题思路一样,但是用EK算法是过不了,提前TLE警告
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 500;
const int INF = 0x3fffffff;
int cap[MAXN][MAXN];//存边的容量,没有边的初始化为0
int path[MAXN], flow[MAXN];
int n;//点的个数,编号[0,n],包括了源点和汇点。
int m, ff, d, st, endd;
queue<int>p;//队列放外面时间是262ms,放进函数里面去之后就322ms,不懂
int bfs()
{
    while(!p.empty())
        p.pop();
    memset(path, -1, sizeof(path));//每次搜索前都把路径初始化成-1
    path[st] = 0;
    flow[st] = INF;//源点可以有无穷的流流进
    p.push(st);
    while (!p.empty())
    {
        int now = p.front();
        p.pop();
        if (now == endd)
            break;
        
        for (int i = 0; i <= n; i++)//枚举所有的点,如果点的编号起始点有变化可以改这里
        {
            if (i != st && path[i] == -1 && cap[now][i])
            {
                flow[i] = flow[now] < cap[now][i] ? flow[now] : cap[now][i];
                p.push(i);
                path[i] = now;
            }
        }
    }
    if (path[endd] == -1)//即找不到汇点上去了。找不到增广路径了
        return -1;
    return flow[endd];
}
int Edmonds_Karp()
{
    int mx_flow = 0;
    int step, pos, pre;
    while ((step = bfs()) != -1)//step是残量
    {
        mx_flow += step;
        pos = endd;//从汇点开始更新流量
        while (pos != st)
        {
            pre = path[pos];
            cap[pre][pos] -= step;
            cap[pos][pre] += step;
            pos = pre;
        }
    }
    return mx_flow;
}
int main()
{
    int food, drink, f_num, d_num;
    while (~scanf("%d%d%d", &m, &ff, &d))
    {
        memset(cap, 0, sizeof(cap));
        n = m * 2 + d + ff + 1;
        st = 0;
        endd = n;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d", &f_num, &d_num);
            for (int j = 0; j < f_num; j++)
            {
                scanf("%d", &food);
                cap[food][i + ff] = 1;
            }

            for (int j = 0; j < d_num; j++)
            {
                scanf("%d", &drink);
                cap[m + ff + i][drink + 2 * m + ff] = 1;
            }
        }

        for (int i = 1; i <= ff; i++)//每种食物和饮料只有一种
            cap[0][i] = 1;

        for (int i = 1; i <= d; i++)
            cap[ff + 2 * m + i][endd] = 1;

        for (int i = 1; i <= m; i++)
            cap[ff + i][ff + m + i] = 1;

        printf("%d
", Edmonds_Karp());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/-citywall123/p/11335126.html