题目描述

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).

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

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

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:

3

说明

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.

Solution:

英文只是来扰人耳目,这道题其实不难,与洛谷P1231类似。由于一只牛最多只能吃一种食物和饮料,所以节点存在限制流量为1,所以要拆点加容量为1的边,然后将食物和饮料分别放在牛的左右两侧,附加炒鸡源S和T,S与食物连容量1的边,T与饮料也连容量1的边,然后跑最大流就好了。。。不懂得可以去看下我P1231的解题博客,这里不多赘述。

代码:

 #include<bits/stdc++.h>
#define il inline
using namespace std;
const int N=,inf=;
queue<int>q;
int s,t,n,f,d,cnt=,h[N],dis[N],ans;
struct edge{
int to,net,v;
}e[N*];
il void add(int u,int v,int w)
{
e[++cnt].to=v,e[cnt].net=h[u],e[cnt].v=w,h[u]=cnt;
e[++cnt].to=u,e[cnt].net=h[v],e[cnt].v=,h[v]=cnt;
}
il bool bfs()
{
memset(dis,-,sizeof(dis));
dis[s]=,q.push(s);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=h[u];i;i=e[i].net)
if(dis[e[i].to]==-&&e[i].v>)dis[e[i].to]=dis[u]+,q.push(e[i].to);
}
return dis[t]!=-;
}
il int dfs(int u,int op)
{
if(u==t)return op;
int flow=,used=;
for(int i=h[u];i;i=e[i].net)
{
int v=e[i].to;
if(dis[v]==dis[u]+&&e[i].v>){
used=dfs(v,min(op,e[i].v));
if(!used)continue;
flow+=used,op-=used;
e[i].v-=used,e[i^].v+=used;
if(!op)break;
}
}
if(!flow)dis[u]=-;
return flow;
}
int main()
{
scanf("%d%d%d",&n,&f,&d);
int u,v,x;t=n*+f+d+;
for(int i=;i<=n;i++)add(i+f,i+n+f,);
for(int i=;i<=f;i++)add(,i,);
for(int i=;i<=d;i++)add(i+n*+f,t,);
for(int i=;i<=n;i++){
scanf("%d%d",&u,&v);
for(int j=;j<=u;j++)scanf("%d",&x),add(x,i+f,);
for(int j=;j<=v;j++)scanf("%d",&x),add(i+n+f,x+n*+f,);
}
while(bfs())ans+=dfs(s,inf);
cout<<ans;
return ;
}

P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)的更多相关文章

  1. P2891 [USACO07OPEN]吃饭Dining 最大流

    \(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...

  2. P2891 [USACO07OPEN]吃饭Dining

    漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...

  3. 洛谷 P2891 [USACO07OPEN]吃饭Dining

    裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...

  4. 洛谷P2891 [USACO07OPEN]吃饭Dining

    题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...

  5. 「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告

    P2891 [USACO07OPEN]吃饭Dining 题目描述 Cows are such finicky eaters. Each cow has a preference for certain ...

  6. [Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining

    传送门:https://www.luogu.org/problemnew/show/P2891 题面 \ Solution 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...

  7. POJ3281 Dining —— 最大流 + 拆点

    题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  8. [poj3281]Dining(最大流+拆点)

    题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...

  9. bzoj1711[USACO07OPEN]吃饭Dining

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

随机推荐

  1. Struts 2(四):类型转换

    类型转换是Struts 2的一个非常重要的部分,通过类型转换能够将表单参数转换成Java中的各种类型,本文将详细介绍Struts 2的内建类型转换器和自定义类型转换器. 第一节 Struts 2内建类 ...

  2. linux设置定时任务调用接口

    1.设置目录 cd /var/spool/cron 2.编辑文件(当前登录用户,不一定是root) vim root 3.添加内容 0 0 * * * wget http://192.144.141. ...

  3. JS_正则表达式_验证中文字符

    正则表达式:"^[\u4e00-\u9fa5]{0,}$" . "/^[\u4E00-\u9FA5]{1,5}$/" 的含义: 在JS里,\uXXXX 是转义字 ...

  4. CentOS 下 Java 的下载、安装、配置

    CentOS 下 Java 的下载.安装.配置 系统: CentOS 7 x86_64 Java 版本: 1.8.0_171 本文将 Java 目录放在 /usr/local/java 文件夹下,读者 ...

  5. 《Node.js核心技术教程》学习笔记

    <Node.js核心技术教程>TOC \o "1-3" \h \z \u 1.章模块化编程 2019.2.19 13:30' PAGEREF _101 \h 1 08D ...

  6. Ubuntu16.04使用Tarball安装ntp

    最近在学习linux,看书上例子(鸟哥的linux私房菜 P674),使用Tarball来安装ntp,出了点问题,提示错误,使用 ./configure 来检测程序时,出现如下提示: 提示少了 ope ...

  7. 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)

    import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...

  8. Dubbo背景和简介

    转载出处 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. 单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一起,以减少部署节点和成本. 缺点:单一的 ...

  9. netty初认识

    Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socket的 ...

  10. Alpha阶段中间产物——GUI Prototype、WBS及PSP

    作业地址:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1224 内容: GUI Prototype 我的书架 我的书架→添加图书 ...