题目描述

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. Zabbix学习之路(四)之Web监控

    1.Zabbix监控web服务器访问性能 zabbix 对 web 性能的监控,通过它可以了解 web 站点的可用性以及性能.最终将各项指标绘制到图形中,这样我们可以了解到一个站点的下载速度.响应速度 ...

  2. xgboost算法教程(两种使用方法)

    标签: xgboost 作者:炼己者 ------ 欢迎大家访问我的简书以及我的博客 本博客所有内容以学习.研究和分享为主,如需转载,请联系本人,标明作者和出处,并且是非商业用途,谢谢! ------ ...

  3. cocos2dx2.0 帧动画的创建和播放过程 深入分析

    一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...

  4. 怎样安装TortoiseGit

    TortoiseGit是基于Windows的Git图形化工具 访问 https://tortoisegit.org/

  5. mybatis 加载配置文件的方法

    一.  使用sqlSessionFactory 的 mapperLocations 进行加载 <!-- SessionFactory --> <bean id="sqlSe ...

  6. winform圆角窗体实现

    winform圆角窗体实现 1.窗体的FormBorderStyle设置成None,不要控制边框 2.TransparencyKey和BackColor颜色设置成相同的,这样,窗体就透明了 3.以此为 ...

  7. SQL Server数据库复制

    事务复制 事务复制是一种复制类型,对订阅服务器上应用的初始数据快照,然后当发布服务器上发生数据修改时,将捕获到个别的事务并传播到订阅服务. 事务复制的原理是先将发布服务器数据库中的初始快照发送到各订阅 ...

  8. lintcode-201-线段树的构造

    201-线段树的构造 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start 和 e ...

  9. 第6题 ZigZag转换

    题目描述如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ro ...

  10. 【leetcode】198.HouseRobber

    198.HouseRobber You are a professional robber planning to rob houses along a street. Each house has ...