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

Http

POJ:https://vjudge.net/problem/POJ-3281

Source

网络流

题目大意

有n只牛,F种食物,D种饮料,每一只牛喜欢若干食物和饮料。现在要给每一只牛分配食物和饮料,每一种食物或饮料只够一只牛吃。若一只牛吃到了它喜欢的食物和饮料(注意是两个都要符合),它就会很开心。现在求最多能使多少只牛开心

解决思路

这道题与Luogu1402有些类似,但不知道为什么我用二分图的方法会WrongAnswer。所以我们用网络流最大流来解决。

对于每一只牛i我们把其拆成两个点i和i+n,在i与i+n中连一条流量为1的边。这样是为了保证一只牛只吃到一种食物和一种饮料。再在对应的食物与牛i连容量为1的边,在对应的牛与饮料连容量为1的边。最后再连上超级源点和超级汇点就可以了。

虽然笔者是使用EK算法实现的网络流最大流,但是更推荐效率更优的Dinic算法,具体请移步我的这篇文章

代码

EK算法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; const int maxN=510;
const int maxM=2147483647;
const int inf=2147483647; int n,F,D;
int G[maxN][maxN];
int Flow[maxN];
int Path[maxN]; bool bfs(); int main()
{
memset(G,0,sizeof(G));
scanf("%d%d%d",&n,&F,&D);
for (int i=1;i<=n;i++)//先在拆出来的牛点之间连边
G[i][i+n]=1;
for (int i=1;i<=n;i++)
{
int n1,n2;
scanf("%d%d",&n1,&n2);
for (int j=1;j<=n1;j++)
{
int v;
scanf("%d",&v);
G[2*n+v][i]=1;//连牛与食物
}
for (int j=1;j<=n2;j++)
{
int v;
scanf("%d",&v);
G[i+n][2*n+F+v]=1;//连牛与饮料
}
}
for (int i=1;i<=F;i++)
G[0][n*2+i]=1;//连源点
for (int i=1;i<=D;i++)
G[n*2+F+i][n*2+F+D+1]=1;//连汇点
int Ans=0;
while (bfs())//EK算法
{
int di=Flow[n*2+F+D+1];
int now=n*2+F+D+1;
int last=Path[now];
while (now!=0)
{
G[last][now]-=di;
G[now][last]+=di;
now=last;
last=Path[now];
}
Ans++;
}
cout<<Ans<<endl;
return 0;
} bool bfs()
{
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
Flow[0]=inf;
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);
do
{
int u=Q.front();
Q.pop();
for (int i=0;i<=2*n+F+D+1;i++)
{
if ((Path[i]==-1)&&(G[u][i]>0))
{
Path[i]=u;
Q.push(i);
Flow[i]=min(Flow[u],G[u][i]);
}
}
}
while (!Q.empty());
if (Flow[n*2+F+D+1]==0)
return 0;
return 1;
}

POJ 3281 Dining (网络流)的更多相关文章

  1. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  2. POJ 3281 Dining 网络流最大流

    B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  3. POJ 3281 Dining (网络流之最大流)

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...

  4. POJ 3281 Dining[网络流]

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  5. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...

  6. POJ 3281 Dining(网络流-拆点)

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  7. POJ 3281 Dining(最大流)

    POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...

  8. POJ 3281 Dining(网络流拆点)

    [题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...

  9. poj 3281 Dining(网络流+拆点)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20052   Accepted: 8915 Descripti ...

随机推荐

  1. 20155239 《网络对抗》Exp4 恶意代码分析

    20155239 <网络对抗>Exp4 恶意代码分析 使用schtasks指令监控系统运行 先在C盘目录下建立一个netstatlog.bat文件,用来将记录的联网结果格式化输出到nets ...

  2. POJ 1988&&2236

    并查集,如果只是朴素的路径压缩,那么也就是一句话的事情. 但是,一般都没有这种仁慈的裸题(假的,多了去了) 1988:带权并查集,贼鸡儿像Luogu的那道杨威利的并查集(好像是叫银河英雄传说) 开两个 ...

  3. python 获取文件和文件夹大小

    1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...

  4. 记一次Spring的aop代理Mybatis的DAO所遇到的问题

    由来 项目中需要实现某个订单的状态改变后然后推送给第三方的功能,由于更改状态的项目和推送的项目不是同一个项目,所以为了不改变原项目的代码,我们考虑用spring的aop来实现. 项目用的是spring ...

  5. R实战 第九篇:数据标准化

    数据标准化处理是数据分析的一项基础工作,不同评价指标往往具有不同的量纲,数据之间的差别可能很大,不进行处理会影响到数据分析的结果.为了消除指标之间的量纲和取值范围差异对数据分析结果的影响,需要对数据进 ...

  6. java写个自己的mvc框架学习笔记

    1. 介绍 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的 ...

  7. Visual Studio控制台程序输出窗口一闪而过的解决方法

    转载大牛的博客,自己也遇到了类似的问题,解决方法很详细,也很管用   刚接触 Visual Studio的时候大多数人会写个Hello World的程序试一下,有的人会发现执行结束后输出窗口会一闪而过 ...

  8. idou老师教你学Istio :如何用istio实现监控和日志采集

    大家都知道istio可以帮助我们实现灰度发布.流量监控.流量治理等功能.每一个功能都帮助我们在不同场景中实现不同的业务.那Istio是如何帮助我们实现监控和日志采集的呢? 这里我们依然以Bookinf ...

  9. [Hanani]JAVA大数相关学习记录

    1.Basic remains 题目链接 涉及内容: |大数读入|大数模|大数进制读入时转化为十进制|大数输出时转化为其他进制输出| import java.io.*; import java.mat ...

  10. [JSP] c:forEach 如何输出序号

    关键在于<c:forEach>的varStatus属性,具体代码如下: <table width="500" border="0" cells ...