P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)
题目描述
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
输入输出样例
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
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(最大流+拆点)的更多相关文章
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- P2891 [USACO07OPEN]吃饭Dining
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...
- 洛谷 P2891 [USACO07OPEN]吃饭Dining
裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...
- 洛谷P2891 [USACO07OPEN]吃饭Dining
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- 「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告
P2891 [USACO07OPEN]吃饭Dining 题目描述 Cows are such finicky eaters. Each cow has a preference for certain ...
- [Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining
传送门:https://www.luogu.org/problemnew/show/P2891 题面 \ Solution 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...
- POJ3281 Dining —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- [poj3281]Dining(最大流+拆点)
题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...
- bzoj1711[USACO07OPEN]吃饭Dining
题意 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮 ...
随机推荐
- Android:反编译apk
一.所需工具 1. apktool (1)作用:获取资源文件,例如图片.布局文件 (2)下载地址:https://bitbucket.org/iBotPeaches/apktool/downloads ...
- Spring学习(十三)-----Spring 表达式语言(Spring EL)
本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...
- 进阶篇:4.1)DFA设计指南:简化产品设计(kiss原则)
本章目的:理解kiss原则,明确如何简化产品的设计. 1.前言:kiss原则,优化产品的第一原则 如果要作者选出一个优化产品的最好方法,那一定是kiss原则莫属.从产品的整体设计到公差的分析,kiss ...
- NO--10今天带大家回忆回忆“闭包”吧!
对于‘闭包,我相信很多人都掉进过这个坑里,也相信很多人没能详细的理解这个问题,今天带大家再次走进闭包: 写这篇文章时的心情是十分忐忑的,因为对于我们今天的主角:闭包,很多小伙伴都写过关于它的文章,相信 ...
- div不设置高度背景颜色或外边框不能显示的解决方法
在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在浏览时出现最外层Div的背景颜色和边框不起作用的问题. 大体结构<div class="oute ...
- win2003系统网络安装——基于linux+pxe+dhcp+tftp+samba+ris
原文发表于:2010-09-16 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...
- 爬虫:Scrapy12 - Stats Collection
Scrapy 提供了方便的收集数据的机制.数据以 key/value 方式存储,值大多是计数值.该机制叫做数据收集器(Stats Collector),可以通过 Crawler API 的属性 sta ...
- css 元素水平垂直方向居中
html部分 <div class="parent"> <div class="child"> - -居中- - </div> ...
- 四则运算(Android)版
实验题目: 将小学四则运算整合成网页版或者是Android版.实现有无余数,减法有无负数.... 设计思路: 由于学到的基础知识不足,只能设计简单的加减乘除,界面设计简单,代码量少,只是达到了入门级的 ...
- HDU 5656 CA Loves GCD 01背包+gcd
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...