洛谷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 ...
随机推荐
- Storm Zookeeper
本文记录了storm 1.1.0 在zookeeper中保存的信息. 下面的图是在[4]的基础上进行修改的. /-storm -- storm在zookeeper上的根目录 | |-/assignme ...
- C++笔试专题一:运算符重载
一:下面重载乘法运算符的函数原型声明中正确的是:(网易2016校招) A:MyClass operator *(double ,MyClass); B:MyClass operator *(MyCla ...
- Map<String,String>转换json字符串
import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; public class testJson ...
- const 和 pointer
一般的: const对pointer的修饰有两种: const type * p/type const * p:表示指针指向的变量的值不能改变,无论指针改变为指向哪一个变量 type * const ...
- [HNOI2012]矿场搭建(割点)
[HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出 ...
- Android Volley 具体解释 Google公布的一套用于网络通信的工具库
下载地址:git clone https://android.googlesource.com/platform/frameworks/volley 或 : https://github.com/mc ...
- Servlet具体解释
Servlet具体解释 基本概述 Session在计算机中,尤其是在网络应用中,称为"会话控制".在计算机专业术语中.Session是指一个终端用户与交互系统进行通信的时间间隔,通 ...
- Qt中事件分发源码剖析
Qt中事件分发源码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,而且进行分发,这些都是在exec中进行的. 以下举例说明: 1)首先看看以下一段演示样例代码: ...
- vue14 自定义过滤器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- FSM的几种策略
FSM是什么?FSM就是Finite(有限) State(状态) 机(Machine)的缩写.(之所以中英文混写,是为了强调学懂FSM的原理是根本,刻意去采用“几段式”的写法并不重要) riple F ...