POJ 3281 Dining (网络流)

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

题意:有 F 种食物和 D 种饮料, 每种食物或饮料只能供一头牛享用, 且每头牛只享用一种食物和一种饮料。现在有 N 头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料 。(1 <= F <=100, 1 <= D <= 100, 1 <= N <= 100)

分析:(引用建模汇总的分析,人懒233)此题的建模方法比较有开创性。 以往一般都是左边一个点集表示供应并与源相连,右边一个点集表示需求并与汇相连。现在不同了,供应有两种资源,需求仍只有一个群体,怎么办?其实只要仔细思考一下最大流的建模原理,此题的构图也不是那么难想。最大流的正确性依赖于它的每一条 s-t 流都与一种实际方案一一对应。那么此题也需要用 s-t 流将一头牛和它喜欢的食物和饮料“串”起来,而食物和饮料之间没有直接的关系,自然就想到把牛放在中间,两边是食物和饮料,由 s, t 将它们串起来构成一种分配方案。至此建模的方法也就很明显了:每种食物 i 作为一个点并连边(s, i, 1),每种饮料 j 作为一个点并连边(j, t, 1),将每头牛 k拆成两个点 k’, k’’并连边(k’, k’’, 1), (i, k’, 1), (k’’, j, 1),其中 i, j 均是牛 k 喜欢的食物或饮料。求一次最大流即为结果。

想想我们平时在建图时源点和汇点往往都是作为限制所用,此题更是利用这一点,把源点和汇点两个限制用起来,源点限制食物,汇点限制饮料,对吧,中间的就是选择食物和饮料的牛牛了,如果一头牛选择了某种食物和饮料,那么是不是就从源点到汇点用一条线连起来的感觉,有几条线就有几头牛能满足,至于为什么必须拆点,因为每头牛只能选择一种饮料喝食物,如果你只是在源点到食物之间限制容量为1是不够的,你画图看看, 如果不拆点直接连,是不是可以有多个食物连向一头牛,一头牛也可以连向多种饮料。。。

废话不多说,上代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#define maxn 2000
#define LL long long
#define inf 0x7fffffff
#define INF 1e18
using namespace std;
struct edge
{
    int from,to,cap,flow;
};
vector<edge> edges;
vector<int> G[maxn];
int d[maxn],cur[maxn];
bool vis[maxn];
int s,t;
void init()
{
    for(int i=0;i<maxn;i++) G[i].clear();
    edges.clear();
}
void add(int from,int to,int cap)
{
    edges.push_back((edge){from,to,cap,0});
    edges.push_back((edge){to,from,0,0});
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool bfs()
{
    memset(vis,false,sizeof(vis));
    memset(d,0,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0,vis[s]=true;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0;i<G[x].size();i++)
        {
            edge& e=edges[G[x][i]];
            if(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=true;
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}
int dfs(int x,int a)
{
    if(x==t||a==0) return a;
    int flow=0,f;
    for(int& i=cur[x];i<G[x].size();i++)
    {
        edge& e=edges[G[x][i]];
        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
        {
            e.flow+=f;
            edges[G[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int maxflow()
{
    int flow=0;
    while(bfs())
    {
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,inf);
    }
    return flow;
}
int main()
{
    int N,F,D,f,d,x;
    while(scanf("%d%d%d",&N,&F,&D)==3)
    {
        init();
        s=0,t=F+D+N+410;
        for(int i=1;i<=F;i++) add(s,i,1);
        for(int i=1;i<=D;i++) add(i+F,t,1);
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&f,&d);
            add(i+F+D,i+F+D+400,1);
            for(int j=1;j<=f;j++)
            {
                scanf("%d",&x);
                add(x,i+F+D,1);
            }
            for(int j=1;j<=d;j++)
            {
                scanf("%d",&x);
                add(i+F+D+400,x+F,1);
            }
        }
        printf("%d\n",maxflow());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值