poj 3281 Dining 【圖論-網絡流-最大流-EK&Ford-Fulkerson】

                                        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 + 10

int 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 + 10

int 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章