[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 ...
随机推荐
- DDD 架构分层,MQ消息要放到那一层处理?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 本文的宗旨在于通过简单干净实践的方式教会读者,使用 Docker 配置 RocketMQ 并在 ...
- 《小白WEB安全入门》02. 开发篇
@ 目录 初识HTML潜在漏洞 初识CSS潜在漏洞 初识JS潜在漏洞 初识后端潜在漏洞 后端能做什么 后端种类 后端框架 潜在漏洞 本系列文章只叙述一些超级基础理论知识,极少有实践部分 本文涉及到的语 ...
- Antd Form表单中Input输入框 在IE9下按下任何按键都会报校验失败
antd Form表单中Input输入框 在IE9下按下任何按键都会报校验失败,导致输入框输入不了任何内容! 可能你的react及react-dom版本由于过高导致antd组件不能兼容,需要对reac ...
- UM 百度富文本编辑器自定义图片上传路径
UM 百度富文本编辑器自定义图片上传路径 因为公司要做图文编辑,选择了UM,但是直接存入Tomcat根目录下,不满足业务需求需要存入服务器上. 一.需要注意的是在um的JSP目录下已经存在了Uploa ...
- 达梦数据库-DW-国产化--九五小庞
武汉达梦数据库股份有限公司成立于2000年,是国内领先的数据库产品开发服务商,国内数据库基础软件产业发展的关键推动者.公司为客户提供各类数据库软件及集群软件.云计算与大数据等一系列数据库产品及相关技术 ...
- 在线问诊 Python、FastAPI、Neo4j — 创建 疾病节点
目录 疾病数据 创建节点 根据检查结果.医生的临床经验得出疾病 疾病数据 disease_data.csv 建议值用""引起来.避免中间有,号造成误识别 疾病 "干眼&q ...
- 贝塞尔曲线的切线及其AABB问题
贝塞尔曲线的切线及其AABB问题 先聊点别的 2023 年抖音上居然还看到很多前端培训 各种直播前端教学(虽然是录播)但看起来还是有大批前往前端卷啊 说明了什么,很可能说明其它行业更难卷 这不是行业不 ...
- Redis系列23:性能优化指南
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- MySQL系列之主从复制基础——企业高可用性标准、主从复制简介、主从复制前提(搭建主从的过程)、主从复制搭建、主从复制的原理、主从故障监控\分析\处理、主从延时监控及原因
文章目录 0.企业高可用性标准 *** 0.1 全年无故障率(非计划内故障停机) 0.2 高可用架构方案 1. 主从复制简介 ** 2. 主从复制前提(搭建主从的过程) *** 3. 主从复制搭建(C ...
- 下载、安装CAN-EYE植被参数工具
本文介绍植被指数计算软件CAN-EYE的下载.安装方法. CAN-EYE软件是由法国国家农业研究院(French National Institute of Agricultural Rese ...