POJ3281 Dining(拆点+最大流+EK算法)

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
1
2
3
4
5
Sample Output
3

题意:

农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100)
种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?

因为每种食物或者每种饮料都只能分配给一头牛,所以我们要进行拆点,来保证饮料和食物只从一条边上面经过,不拆点不能保证牛只能选择一种食物和一种饮料这一条件。即限定牛结点的容量为1。那么我们来建图:

建立一个超级源,跟每种食物之间连一条容量为1的边;
建立一个超级汇,它与每种饮料之间有一条容量为1的边;
将每头牛都拆分成两个点C1、C2,两点之间有一条容量为1的边;
若一头牛喜欢食物f,就将其对应的C1点与f连接起来,容量为1,若一头牛喜欢饮料d,同理将C2与d连接起来。
我们用来表示牛,食物和饮料的范围为:

牛:11~2∗n2∗n
食物:2∗n+12∗n+1~2∗n+f2∗n+f
饮料:2∗n+f+12∗n+f+1~2∗n+f+d2∗n+f+d
超级源点:0
超级汇点:2∗n+f+d+1
比如样例,所建的图:
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=400+20;
int g[N][N],pre[N];
bool vis[N];
int st,ed,p;
bool bfs(int s,int t)
{
    mem(pre,-1);
    mem(vis,false);
    queue<int>q;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=st; i<=ed; i++)
        {
            if(!vis[i]&&g[now][i]>0)
            {
                vis[i]=true;
                pre[i]=now;
                if(i==t)
                    return true;
                q.push(i);
            }
        }
    }
    return false;
}
int EK(int s,int t)
{
    int v,w,d,maxflow=0;
    while(bfs(s,t))
    {
        v=t;
        d=inf;
        while(v!=s)
        {
            w=pre[v];
            d=min(d,g[w][v]);
            v=w;
        }
        maxflow+=d;
        v=t;
        while(v!=s)
        {
            w=pre[v];
            g[w][v]-=d;
            g[v][w]+=d;
            v=w;
        }
    }
    return maxflow;
}
int main()
{
    int n,f,d,a,b,x;
    scanf("%d%d%d",&n,&f,&d);
    for(int i=1; i<=n; i++) g[2*i-1][2*i]=1;//拆点
    for(int i=1; i<=f; i++) g[0][2*n+i]=1;//食物的标号范围:2*n+1~2*n+f
    for(int i=1; i<=d; i++) g[2*n+f+i][2*n+f+d+1]=1;//饮料的标号范围:2*n+f+1~2*n+f+n
    st=0;
    ed=2*n+f+d+1;
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",&a,&b);
        for(int j=1; j<=a; j++)
        {
            scanf("%d",&x);
            g[2*n+x][2*i-1]=1;
        }
        for(int j=1; j<=b; j++)
        {
            scanf("%d",&x);
            g[2*i][2*n+f+x]=1;
        }
    }
    printf("%d\n",EK(st,ed));
    return 0;
}
发布了54 篇原创文章 · 获赞 28 · 访问量 7290

猜你喜欢

转载自blog.csdn.net/jiangkun0331/article/details/99687425