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

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.

题意:就是农夫约翰有很多头牛,然后他做了f份食物和d份饮料,他忘了每头牛的喜好。每头牛喜欢特定序号的食物和饮料,所以农夫约翰想问问你能不能找到满足最多头牛都满意都能吃能喝的场景。

思路:这道题的话,在网络流专题里,所以就直接想了网络流的做法。这道题的建图就很巧妙,因为每头牛都有对应的喜好的食物和饮料,所以我们可以把每头牛分成两个点,一个点连着f(食物),一个点连着d(饮料),再在每头牛的两个点之间建一条容量为1的边。然后再在f(食物)点左边建一个源点s,再在d(饮料)点右边建一个汇点t。具体效果图如下。

                          

下面附上AC代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <iostream>
#include <queue>
const int maxx=1010;
const int inf=0x3f3f3f3f;
using namespace std;
int c[maxx][maxx],pre[maxx],flow[maxx];
int n;//点的个数,编号0-n.n包括了源点和汇点。
int bfs(int s,int t)
{
    queue<int>q;
    while(!q.empty())
        q.pop();
    memset(pre,-1,sizeof(pre));
    pre[s]=0;
    flow[s]=inf;
    q.push(s);
    while(!q.empty())
    {
        int index=q.front();
        q.pop();
        if(index==t)
            break;  //枚举所有的点,如果点的编号起始点有变化可以改这里
        for(int i=0; i<=n; i++)
        {
            if(c[index][i]>0 && pre[i]==-1 && i!=s)
            {
                pre[i]=index;
                flow[i]=min(c[index][i],flow[index]);
                q.push(i);
            }
        }
    }
    if(pre[t]==-1)//即找不到汇点上去了。找不到增广路径了
        return -1;
    else
        return flow[t];
}
int max_flow(int u,int v)
{
    int sumflow=0,ans;
    while(ans=bfs(u,v)!=-1)
    {
        int k=v;
        while(k!=u)
        {
            int last=pre[k];
            c[last][k]-=ans;
            c[k][last]+=ans;
            k=last;
        }
        sumflow+=ans;
    }
    return sumflow;
}
int main()
{
    int N,f,d;
    while(~scanf("%d%d%d",&N,&f,&d))
    {
        memset(c,0,sizeof(c));
        n=f+d+2*N+1;
        for(int i=1; i<=f; i++)
            c[0][i]=1;
        for(int i=f+2*N+1; i<=f+d+2*N; i++)
            c[i][n]=1;
        for(int i=1; i<=N; i++)
            c[f+2*i-1][f+2*i]=1;
        int a,b;
        int x;
       for(int i=1;i<=N;i++)
       {
           scanf("%d%d",&a,&b);
           while(a--)
           {
               scanf("%d",&x);
               c[x][f+2*i-1]=1;
           }
           while(b--)
           {
               scanf("%d",&x);
               c[f+2*i][f+2*N+x]=1;
           }
       }
       printf("%d\n",max_flow(0,n));
    }
    return 0;
}
发布了204 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43846139/article/details/103410450