poj 3281 Dining 【图论-网络流-最大流-EK&Ford-Fulkerson】

来源:互联网 发布:卷皮和淘宝质量哪个好 编辑:程序博客网 时间:2024/05/16 09:44
                                        Dining                    Time Limit: 2000MS      Memory Limit: 65536K

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.

题目大意:农场主有N头牛,并且准备了F种食物,D种饮料,每一头牛会喜欢若干种食物和饮料,但它只能选择一种食物和一种饮料,且每种食物和饮料都只够一头牛选择,问怎样分配能使得食物和饮料都能得到的牛的数量最多,求这个数。

思路: 拆点-拆牛点
0为源点,1~f为食物点,f+1~f+n为左边的牛点,f+n+1~f+2 * n为右边的牛点,f+2 * n+1~f + 2 * n + d 为饮料点,f+2 * n + d + 1 为汇点
源点->食物->牛->牛->饮料->汇点
以样例做网络图为:
样例构图
图中所有边都是单向边,都是由源点方向指向汇点方向

知识点: 最大流、二分图多重匹配

所用算法: EK、Ford-Fulkerson

AC代码1: EK

//EK//Memory 1168K  Time 157MS# include <iostream># include <cstdio># include <cstring>using namespace std;# define MAXN 505# define INF 1e9 + 10int map[MAXN][MAXN];int que[MAXN];int used[MAXN];int pre[MAXN];int f, n, d;int min(int a, int b){    return a > b ? b : a;}int Bfs(int s, int t){    int head = 1;    int tail = 1;    memset(pre, -1, sizeof(pre));    que[tail++] = s;    pre[s] = -1;    while (tail > head)    {        int u = que[head++];        for (int i = 0; i <= t; i++)        {            if (pre[i] == -1 && map[u][i] > 0)            {                pre[i] = u;                if (i == t)                {                    return 1;                }                que[tail++] = i;            }        }    }    return 0;}int Maxflow(int s, int t){    int maxflow = 0;    while (Bfs(s, t))    {        int i;        int minflow = INF;        for (i = t; i != s; i = pre[i])        {            minflow = min(minflow, map[pre[i]][i]);        }        for (i = t; i != s; i = pre[i])        {            map[pre[i]][i] -= minflow;            map[i][pre[i]] += minflow;        }        maxflow += minflow;    }    return maxflow;}int main(void){    while (~scanf("%d %d %d", &n, &f, &d))    {        int i, j;        memset(map, 0, sizeof(map));        for (i = 1; i <= f; i++)        {            map[0][i] = 1;        }        for (i = 1; i <= n; i++)        {            map[f+i][f+n+i] = 1;        }        for (i = 1; i <= d; i++)        {            map[f+2*n+i][f+2*n+d+1] = 1;        }        int fn, dn, fth, dth;        for (i = 1; i <= n; i++)        {            scanf("%d %d", &fn, &dn);            for (j = 1; j <= fn; j++)            {                scanf("%d", &fth);                map[fth][f+i] = 1;            }            for (j = 1; j <= dn; j++)            {                scanf("%d", &dth);                map[f+n+i][f+2*n+dth] = 1;            }        }        printf("%d\n", Maxflow(0, f+2*n+d+1));    }    return 0;}

AC代码2:Ford-Fulkerson

//Ford-Fulkerson//Memory 1172K  Time 16MS# include <iostream># include <cstdio># include <cstring>using namespace std;# define MAXN 505# define INF 1e9 + 10int map[MAXN][MAXN];int que[MAXN];int used[MAXN];int pre[MAXN];int f, n, d;int N;int min(int a, int b){    return a > b ? b : a;}int Dfs(int s, int t, int f){    if (s == t)    {        return f;    }    for (int i = 0; i <= t; i++)    {        if (map[s][i] > 0 && !used[i])        {            used[i] = 1;            int minf = f < map[s][i] ? f : map[s][i];            int d = Dfs(i, t, minf);            if (d > 0)            {                map[s][i] -= d;                map[i][s] += d;                return d;            }        }    }    return 0;}int Maxflow(int s, int t){    int maxflow = 0;    while (1)    {        memset(used, 0, sizeof(used));        int f = Dfs(s, t, INF);        if (!f)        {            return maxflow;        }        maxflow += f;    }}int main(void){    while (~scanf("%d %d %d", &n, &f, &d))    {        int i, j;        memset(map, 0, sizeof(map));        for (i = 1; i <= f; i++)        {            map[0][i] = 1;        }        for (i = 1; i <= n; i++)        {            map[f+i][f+n+i] = 1;        }        for (i = 1; i <= d; i++)        {            map[f+2*n+i][f+2*n+d+1] = 1;        }        int fn, dn, fth, dth;        for (i = 1; i <= n; i++)        {            scanf("%d %d", &fn, &dn);            for (j = 1; j <= fn; j++)            {                scanf("%d", &fth);                map[fth][f+i] = 1;            }            for (j = 1; j <= dn; j++)            {                scanf("%d", &dth);                map[f+n+i][f+2*n+dth] = 1;            }        }        N = f+2*n+d+1;        printf("%d\n", Maxflow(0, f+2*n+d+1));    }    return 0;}
0 0
原创粉丝点击