P2891 [USACO07OPEN]吃饭Dining
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891
题目描述
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
输入输出样例
说明
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.
- // luogu-judger-enable-o2
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #include<queue>
- using namespace std;
- const int N=1e4+;
- const int M=3e4+;
- const int INF=0x7fffffff;
- int n,F,D,S,T;
- int head[N],num_edge;
- struct Edge
- {
- int v,flow,nxt;
- }edge[M<<];
- inline int read()
- {
- char c=getchar();int num=,f=;
- for(;!isdigit(c);c=getchar())
- f=c=='-'?-:f;
- for(;isdigit(c);c=getchar())
- num=num*+c-'';
- return num*f;
- }
- inline void add_edge(int u,int v,int flow)
- {
- edge[++num_edge].v=v;
- edge[num_edge].flow=flow;
- edge[num_edge].nxt=head[u];
- head[u]=num_edge;
- }
- int dep[N];
- inline bool bfs()
- {
- memset(dep,,sizeof(dep));
- queue<int> que;
- que.push(S),dep[S]=;
- int now,v;
- while(!que.empty())
- {
- now=que.front(),que.pop();
- for(int i=head[now];i;i=edge[i].nxt)
- {
- if(edge[i].flow)
- {
- v=edge[i].v;
- if(dep[v])
- continue;
- dep[v]=dep[now]+;
- if(v==T)
- return ;
- que.push(v);
- }
- }
- }
- return ;
- }
- int dfs(int now,int flow)
- {
- if(now==T)
- return flow;
- int outflow=,tmp,v;
- for(int i=head[now];i;i=edge[i].nxt)
- {
- if(edge[i].flow)
- {
- v=edge[i].v;
- if(dep[v]!=dep[now]+)
- continue;
- tmp=dfs(v,min(flow,edge[i].flow));
- if(tmp)
- {
- outflow+=tmp;
- flow-=tmp;
- edge[i].flow-=tmp;
- edge[i^].flow+=tmp;
- if(!flow)
- return outflow;
- }
- }
- }
- dep[now]=;
- return outflow;
- }
- int main()
- {
- num_edge=;
- n=read(),F=read(),D=read();
- T=F+D+n*+;
- for(int i=;i<=n;++i)
- {
- add_edge(i+F,i+F+n,);
- add_edge(i+F+n,i+F,);
- }
- for(int i=;i<=F;++i)
- {
- add_edge(S,i,);
- add_edge(i,S,);
- }
- for(int i=;i<=D;++i)
- {
- add_edge(i+F+*n,T,);
- add_edge(T,i+F+n*,);
- }
- for(int i=,a,b,c;i<=n;++i)
- {
- b=read(),c=read();
- while(b--)
- {
- a=read();
- add_edge(a,i+F,);
- add_edge(i+F,a,);
- }
- while(c--)
- {
- a=read();
- add_edge(i+F+n,a+F+n*,);
- add_edge(a+F+n*,i+F+n,);
- }
- }
- int flow=;
- while(bfs())
- flow+=dfs(S,INF);
- printf("%d",flow);
- return ;
- }
P2891 [USACO07OPEN]吃饭Dining的更多相关文章
- P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- 洛谷P2891 [USACO07OPEN]吃饭Dining
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- 洛谷 P2891 [USACO07OPEN]吃饭Dining
裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...
- 「洛谷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 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...
- [USACO07OPEN]吃饭Dining
嘟嘟嘟 这应该是网络流入门题之一了,跟教辅的组成这道题很像. 把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案. #include<cstdio> #i ...
- bzoj1711[USACO07OPEN]吃饭Dining
题意 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮 ...
- BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配
题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...
随机推荐
- 在vue中使用ElementUI
完整引用ElementUI: 安装:在需要使用到的vue项目目录下,使用npm下载安装: npm/cnpm i element-ui -S/--save <!-- 引入样式 --> < ...
- dfs的剪枝优化
两个剪枝问题 1. 当两点的距离(需要走的步数)大于剩下的时间时 剪去 2.奇偶剪枝问题 如果起点到终点所需走的步数的奇偶性与时间奇偶性不同的时候 剪去 起点到终点步数的奇偶性的判断 首先 明确点的奇 ...
- Flask无法访问(127.0.0.1:5000)的问题解决方法
Flask默认开启的ip地址是:http://127.0.0.1:5000/ 但在运行时可能存在无法访问的问题,特别是当我们在linux服务器上搭建flask时,此时需要将代码修改如下: app.ru ...
- Linux添加vsftp账户和设置目录权限
改变store下面的所有.php文件属主为ftpd[root@www ~]# chgrp ftpd /store/*.php[root@www ~]# chown ftpd /store/*.php ...
- JavaScript Array vs new Array区别
规范说明 When Array is called as a function rather than as a constructor, it creates and initialises a n ...
- 'vue' 不是内部或外部命令
运用cnpm淘宝镜像安装vue-cli,然后输入vue,显示“'vue' 不是内部或外部命令”,然后百度查找方法,解决办法如下: 虽然电脑是64位的电脑,然后node我也下载安装的是64位,然后,我重 ...
- CentOS7 安装记录
起因是想自建一个本地笔记云存储,按照网上的教程搭建,卡在了其中的一个步骤上(文章见https://www.laobuluo.com/1542.html),卡在了如下图的位置,google了一番解决的办 ...
- Linux的进程管理基本指令
在Linux操作系统中,进程是指一个程序的运行实例,它需要存储器来存储程序本身及其操作数据.内核负责创建和跟踪进程.当程序运行时,内核首先准备好一些内存,将可执行代码从文件系统加载到内存里,然后开始运 ...
- 13. 请看TED 的演讲, 谈谈你对压力的看法,以及怎么和别人合作, 帮助别人,把压力转化为动力,在互相帮助的环境中成长。------------答题者:徐潇瑞
看了ted的演讲,我觉得压力就像一根弹簧,有多大的压力,它就有多大的弹力:现实中只要你学会用一种永远不服输的顽强精神,去对待人生和社会中遇到的一切困难与挫折,宠辱不惊的看云卷云舒,悟潮起潮落.可是存在 ...
- GPU---NVIDIA GPU 计算能力
查询网址:https://developer.nvidia.com/cuda-gpus 使用,makefile文件实例: GPU= CUDNN= OPENCV= OPENMP= DEBUG= ARCH ...