[USACO2007OPENG] Dining G
题目描述
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).
输入格式
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
样例输入 #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.
看到这么小的数据,还有这种选来选去的东西,考虑网络流。
在建图中把牛放在中间,前面放食物,后面放饮料。
由源点向每个食物放一条流量为1的边,每个饮料向汇点放一条流量为1的边。然后把牛拆成两份,中间流量为1.牛和喜欢的饮料食物连边即可。跑一次最大流,每个牛,饮料,食物都会选一次。刚好就可以跑出答案。
#include<bits/stdc++.h>
using namespace std;
int idx=1,hd[200005],k,v[200005],q[200005],thd[200005],kt,l,r,n,f,d,x,y,z,ans,p,qq,s,t;
struct edge{
int v,nxt,f;
}e[2000005];
void add_edge(int u,int v)
{
e[++idx]=(edge){v,hd[u],1};
hd[u]=idx;
e[++idx]=(edge){u,hd[v],0};
hd[v]=idx;
}
int bfs()
{
memcpy(hd,thd,sizeof(hd));
memset(v,0,sizeof(v));
q[l=r=1]=0,v[0]=1;
while(l<=r)
{
for(int i=hd[q[l]];i;i=e[i].nxt)
{
if(e[i].f&&!v[e[i].v])
{
v[e[i].v]=v[q[l]]+1;
q[++r]=e[i].v;
}
}
++l;
}
return v[t];
}
int dfs(int x,int flow)
{
if(x==t)
return flow;
for(int&i=hd[x];i;i=e[i].nxt)
{
if(v[x]+1==v[e[i].v]&&min(flow,e[i].f))
{
if(kt=dfs(e[i].v,min(flow,e[i].f)))
{
e[i^1].f+=kt;
e[i].f-=kt;
return kt;
}
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&n,&f,&d);
s=0,t=1+f+n+d+1;
for(int i=1;i<=f;i++){
add_edge(s,1+i);
}
for(int i=1;i<=d;i++){
add_edge(1+f+n+i,t);
}
for(int i=1;i<=n;i++){
add_edge(1+f+i,1+f+n+d+1+i);
}
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
for(int j=1;j<=x;j++)
{
scanf("%d",&z);
add_edge(1+z,1+f+i);
}
for(int j=1;j<=y;j++)
{
scanf("%d",&z);
add_edge(1+f+n+d+1+i,1+f+n+z);
}
}
memcpy(thd,hd,sizeof(hd));
while(bfs())
while(k=dfs(0,2147483647))
ans+=k;
printf("%d",ans);
return 0;
}
[USACO2007OPENG] Dining G的更多相关文章
- 【题解】[USACO07OPEN]Dining G
\(Link\) \(\text{Solution:}\) 这一题,我们要做到,食物和牛.牛和饮料均为一对一的关系.我们发现这个图不好建立. 经典技巧:将牛拆边,拆成入点和出点,并连容量为\(1\)的 ...
- bzoj5000+的洛谷题号
前言 闲得没事把 bzoj5000+ 在 Luogu 上可找到的题面整理了一下-- 对于我,bzoj 连账号都没有,所以肯定是不清楚 bzoj 题目总数的--因此其实就是手动翻查. 工作量很大,基本不 ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- poj3281 Dining
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14316 Accepted: 6491 Descripti ...
- POJ 3281 Dining
Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...
- POJ 3281 Dining 网络流最大流
B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )
题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...
- POJ 3281 Dining(网络流)
Dining Time Limit: 2000MS Memo ...
随机推荐
- Unity TextMeshPro 添加中文字体遇见的问题以及解决方案
前言 按标准官方教程为 Unity TextMeshPro 添加中文字体时出现了各种奇奇怪怪的问题,于是有了这篇随笔. 中文字体解决方案 以下步骤适用于 TextMeshPro 3.0.6. 字符数量 ...
- 【page cache】简介
目录 page cache 直接 IO 与 缓存 IO Linux IO 栈 Linux 中的具体实现 相关结构体 超级块 super_block 索引节点 inode 文件 file 目录项 den ...
- ETL之apache hop系列4-hop开发数据增量同步功能
ETL增量数据抽取CDC 概念:Change Data Capture,变化的数据捕获,也称:[增量数据抽取](名词解释) CDC是一种实现数据的增量抽取解决方案,是实现[ETL整体解决方案]中的一项 ...
- 微服务下使用maven做多环境配置
分享技术,用心生活 前言:很多项目在开发,提测,上线时都会提前手动改一些配置文件来适应对应环境,麻烦不说了,而且也容易出错:生产环境的配置也容易暴露.基于此,我们基于spring cloud alib ...
- API接口设计规范
说明:在实际的业务中,难免会跟第三方系统进行数据的交互与传递,那么如何保证数据在传输过程中的安全呢(防窃取)?除了https的协议之外,能不能加上通用的一套算法以及规范来保证传输的安全性呢? 下面我们 ...
- 使用 OpenTelemetry 构建 .NET 应用可观测性(2):OpenTelemetry 项目简介
前世今生 OpenTracing OpenTracing 项目启动于 2016 年,旨在提供一套分布式追踪标准,以便开发人员可以更轻松地实现分布式追踪. OpenTracing 定义了一套 Traci ...
- 推荐vue脚手架工具 vue-cli
安装vue-cli之前,需要先装好vue 和 webpack npm install -g vue //全局安装vue npm install -g webpack //全局安装webpack npm ...
- strimzi实战之二:部署和消息功能初体验
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<strimzi实战>系列 ...
- 万字长文详解Java线程池面试题
王有志,一个分享硬核 Java 技术的互金摸鱼侠加入 Java 人的提桶跑路群:共同富裕的Java人 今天是<面霸的自我修养>第 6 篇文章,我们一起来看看面试中会问到哪些关于线程池的问题 ...
- 【Flask模板注入】
[Flask模板注入]--概览 背景 Flask是python语言下的轻量级web应用框架,可以用来开发一些简单的网站.它使用Jinjia2渲染引擎(将html文件存放在templates文件夹中,当 ...