[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 ...
随机推荐
- 数据库中limit 和 offset 使用区别
题:查找最晚入职员工的所有信息 1,SELECT * FROM employees ORDER BY hire_date DESC LIMIT 0,1; 解:对列hire_date分组后升序,从下标( ...
- AI绘画创意文字全流程揭秘,你的终极文字艺术实操宝典
本教程收集于:AIGC从入门到精通教程汇总 AIGC技术不断更新迭代,国内出现了越来越多的新玩法,比如最近大家都在热议的AI绘画创意文字. 过去的一周,我把这些新玩法都研究了一遍,并总结了一套完整的制 ...
- 前端Vue仿企查查 天眼查知识产权标准信息列表组件
引入Vue仿企查查天眼查知识产权标准信息列表组件 随着技术的不断发展,传统的开发方式使得系统的复杂度越来越高.在传统开发过程中,一个小小的改动或者一个小功能的增加可能会导致整体逻辑的修改,造成牵一 ...
- Go学习笔记1
学习路线 2023-Go全链路工程师课纲 https://www.processon.com/view/link/63594cd97d9c0854f9ac855e 一.搭建环境 https://stu ...
- 《Python魔法大冒险》005 魔法挑战:自我介绍机器人
魔法师和小鱼坐在图书馆的一扇窗户旁,窗外的星空闪烁着神秘的光芒.魔法师轻轻地拍了拍小鱼的肩膀. 魔法师: 小鱼,你已经学会了编写简单的魔法程序,现在我要教你如何创造一个有自己思想的机器人,让它能够和我 ...
- C#希尔排序算法
前言 希尔排序简单的来说就是一种改进的插入排序算法,它通过将待排序的元素分成若干个子序列,然后对每个子序列进行插入排序,最终逐步缩小子序列的间隔,直到整个序列变得有序.希尔排序的主要思想是通过插入排序 ...
- flask中cookies的使用
flask中cookies的使用 在Flask中对cookie的处理 1. 设置cookie: 设置cookie,默认有效期是临时cookie,浏览器关闭就失效 可以通过 max_age 设置有效期, ...
- 【matplotlib基础】--3D图形
matplotlib 在1.0版本之前其实是不支持3D图形绘制的. 后来的版本中,matplotlib加入了3D图形的支持,不仅仅是为了使数据的展示更加生动和有趣.更重要的是,由于多了一个维度,扩展了 ...
- 【matplotlib 实战】--面积图
面积图,或称区域图,是一种随有序变量的变化,反映数值变化的统计图表. 面积图也可用于多个系列数据的比较.这时,面积图的外观看上去类似层叠的山脉,在错落有致的外形下表达数据的总量和趋势.面积图不仅可以清 ...
- 【ASP.NET Core】在 Mini-API 中注入服务
经过版本更新,Mini API 的功能逐步完善,早期支持得不太好的 mini API 现在许多特性都可以用了,比如灰常重要的依赖注入. 咱们先来个相当简单的注入测试.来,定义一个服务类,为了偷懒,老周 ...