洛谷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
输入输出样例
说明
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.
最大流问题。
一个比较容易想到的思路就是

但是这样可能出现左边有两个流量经过中间同一个点的情况
所以我们把中间的点拆开
最终的图应该是这样的

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=,INF=*1e8+;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
char c=nc();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=nc();}
while(c>=''&&c<=''){x=x*+c-'';c=nc();}
return x*f;
}
int S=,T=;
struct node
{
int u,v,flow,nxt;
}edge[MAXN*];
int head[MAXN],cur[MAXN],num=;
inline void add_edge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].flow=z;
edge[num].nxt=head[x];
head[x]=num++;
}
inline void AddEdge(int x,int y,int z)
{
add_edge(x,y,z);
add_edge(y,x,);
}int deep[MAXN];
inline bool BFS()
{
memset(deep,,sizeof(deep));
deep[S]=;
queue<int>q;
q.push(S);
while(q.size()!=)
{
int p=q.front();
q.pop();
for(int i=head[p];i!=-;i=edge[i].nxt)
if(!deep[edge[i].v]&&edge[i].flow)
{
deep[edge[i].v]=deep[p]+;q.push(edge[i].v);
if(edge[i].v==T) return ;
}
}
return deep[T];
}
int DFS(int now,int nowflow)
{
if(now==T||nowflow<=) return nowflow;
int totflow=;
for(int &i=cur[now];i!=-;i=edge[i].nxt)
{
if(deep[edge[i].v]==deep[now]+&&edge[i].flow)
{
int canflow=DFS(edge[i].v,min(nowflow,edge[i].flow));
edge[i].flow-=canflow;edge[i^].flow+=canflow;
totflow+=canflow;
nowflow-=canflow;
if(nowflow<=) break;
}
}
return totflow;
}
int Dinic()
{
int ans=;
while(BFS())
{
memcpy(cur,head,sizeof(head));
ans+=DFS(S,INF);
}
return ans;
}
int N,F,D;
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
memset(head,-,sizeof(head));
N=read();F=read();D=read();
for(int i=;i<=N;i++)
{
int Fnum=read(),Dnum=read();
AddEdge(F+i,F+N+i,);
for(int j=;j<=Fnum;j++){int P=read();AddEdge(P,F+i,);}
for(int j=;j<=Dnum;j++){int P=read();AddEdge(F+N+i,*N+F+P,);};
}
for(int i=;i<=F;i++) AddEdge(S,i,);
for(int i=;i<=D;i++) AddEdge(*N+F+i,T,);
printf("%d",Dinic());
return ;
}
洛谷P2891 [USACO07OPEN]吃饭Dining的更多相关文章
- 洛谷 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
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- 「洛谷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 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...
- 洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码
洛谷P2891 Dining P1402 酒店之王[类二分图匹配]题解+代码 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的 ...
- 2018.06.29 洛谷P2890 [USACO07OPEN]便宜的回文(简单dp)
P2890 [USACO07OPEN]便宜的回文Cheapest Palindrome 时空限制 1000ms / 128MB 题目描述 Keeping track of all the cows c ...
- [USACO07OPEN]吃饭Dining
嘟嘟嘟 这应该是网络流入门题之一了,跟教辅的组成这道题很像. 把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案. #include<cstdio> #i ...
随机推荐
- html页面全屏化显示
<html><head><script>// toggle full screen function toggleFullScreen() { if (!docum ...
- cnmp安装失败,报错npm ERR! enoent ENOENT: no such file or directory,
1.cnmp安装失败 2.提示如下: bogon:node_modules liangjingming$ sudo npm install cnpm -g --registry=https://reg ...
- PDO 拿出來的 Float 數據跟数据库中的数据不匹配
数据库中的价格字段是 float 类型的,在 Laravel 中取出会出现这样的情况 数据库:71.9 -> 程序打印:72.0 数据库:75.2 -> 程序打印:75.3 在另外一个测试 ...
- 数据库Tsql语句创建--约束--插入数据
1.创建数据库 use master go if exists(select * from sysdatabases where name='数据库名字') drop database 数据库名字 g ...
- SQL--通过身份证号得到年龄的
/* =======================================创 建 人:CuiYaChao创建日期:2017-08-16功能描述:通过身份证号来计算年龄单元名称: Fun_Ge ...
- iOS开发——根据数组中的字典中的某一元素排序
数组中的元素是字典,字典中的某一个元素,比如说姓名,现在需要按照姓名的首字母来排序,怎么搞? 做法很简单,在字典中加一个元素,保存姓名的首字母,然后用下面的方法排序. - (void)sortWifi ...
- iOS开发——剪切板
剪切板的调用是很简单的,常用的就这两个,不管文字或是图片的粘贴复制,对剪切板的操作就是基于下面两个属性的: [UIPasteboard generalPasteboard].string [UIPas ...
- TCP服务器如何区分不同的用户
CS架构:使用SOCKET(一般是一个整数),当服务器侦听到连接请求的时候,accept会返回一个SOCKET(用于识别不同的连接,可以理解成,SOCKET是区分不同连接的ID).当服务器listen ...
- apache(XAMPP)禁止IP访问的httpd-vhosts.conf设置
httpd-vhosts.conf <virtualhost *:80> ServerName 123.123.123.123 ServerAlias 123.123.123.123 ...
- Python3 利用POP3与smtplib进行计算机远程控制
初习,代码有不足之处,欢迎指出. 跟大家分享的是,通过发送端发送cmd命令,从而对接收端进行cmd命令的控制. #接收端代码 from poplib import POP3 import time,o ...