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)
建模方法
此题的建模方法比较有开创性。以往一般都是左边一个点集表示供应并与源相连, 右边一个点集表示需求并与汇相连。现在不同了,供应有两种资源,需求仍只有 一个群体,怎么办?其实只要仔细思考一下最大流的建模原理,此题的构图也不 是那么难想。最大流的正确性依赖于它的每一条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 喜欢的食物 或饮料。求一次最大流即为结果。
(好久没有10分钟敲完一遍过编译一遍AC的感觉了….)

代码如下:

#include<cstdio>  
#include<cstring> 
#include<string>
#include<algorithm>
#include<map>
#include<queue>  
#include<vector>
using namespace std;  
#define inf 0x3f3f3f3f  
#define maxv 10005
#define maxe 100005 

int nume=0,head[maxv],e[maxe][3];

void inline adde(int i,int j,int c)  
{  
    e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;  
    e[nume++][2]=c; 
    e[nume][0]=i;e[nume][1]=head[j];head[j]=nume;  
    e[nume++][2]=0;  
}  

int ss,tt,n,m,f,d;  
int vis[maxv],lev[maxv]; 

bool bfs()  
{  
    for(int i=0;i<maxv;i++)  
    vis[i]=lev[i]=0;  
    queue<int>q;  
    q.push(ss);  
    vis[ss]=1;  
    while(!q.empty())  
    {  
        int cur=q.front();  
        q.pop();  
        for(int i=head[cur];i!=-1;i=e[i][1])  
        {  
            int v=e[i][0];  
            if(!vis[v]&&e[i][2]>0)  
            {  
                lev[v]=lev[cur]+1;  
                vis[v]=1;  
                q.push(v);  
            }  
        }  
    }  
    return vis[tt];  
}  
int dfs(int u,int minf)  
{  
    if(u==tt||minf==0)return minf;  
    int sumf=0,f;  
    for(int i=head[u];i!=-1&&minf;i=e[i][1])  
    {  
        int v=e[i][0];  
        if(lev[v]==lev[u]+1&&e[i][2]>0)  
        {  
            f=dfs(v,minf<e[i][2]?minf:e[i][2]);  
            e[i][2]-=f;e[i^1][2]+=f;  
            sumf+=f;minf-=f;  
        }  
    }  
    if(!sumf) lev[u]=-1;  
    return sumf;  
}  
int Dinic()  
{  
    int sum=0;  
    while(bfs())sum+=dfs(ss,inf);  
    return sum;  
}  
int main()
{
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        ss=0,tt=505;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=f;++i) adde(ss,i+200,1);
        for(int i=1;i<=d;++i) adde(i+300,tt,1);
        for(int i=1;i<=n;++i) 
        {
            adde(i,i+100,1);
            int F,D;
            scanf("%d%d",&F,&D);
            for(int j=1;j<=F;++j)
            {
                int food;scanf("%d",&food);
                adde(food+200,i,1);
            }
            for(int j=1;j<=D;++j)
            {
                int drink;scanf("%d",&drink);
                adde(i+100,drink+300,1);
            }
        }
        printf("%d\n",Dinic());
    }
    return 0;
}

让我看到你们的双手

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值