【POJ - 3281】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).

Input

Line 1: Three space-separated integers: NF, 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 Fiintegers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

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

Sample Input

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

Sample Output

3

Hint

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.

题目大意:

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100)

解题报告:
以如下的顺序依次建图, 每条边权值均为1
源点 -> 食物 -> 牛 -> 牛 -> 饮料 -> 汇点

因为每个牛只能被选择一次,所以需要将牛拆成两个点并且连一条流量为1的边。

AC代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int n,F,D;
int tot;
struct Edge {int to,ne,w;
} e[100005 * 2];
int head[10005];
int st,ed;
int dis[10050],q[10005];//一共多少个点跑bfs,dis数组和q数组就开多大。 
void add(int u,int v,int w) {e[++tot].to=v;e[tot].w=w;e[tot].ne=head[u];head[u]=tot;
}
bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1;
}
int dfs(int cur,int limit) {//limit为源点到这个点的路径上的最小边权 if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) {		if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow;
}
int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans;
}
int main() 
{cin>>n>>F>>D;int N = F + D + 2*n + 2;st = F + D + 2*n + 1;ed = F + D + 2*n + 2;tot=1;for(int i = 1; i<=ed; i++) head[i] = -1;for(int i = 1; i<=F; i++) add(st,i,1),add(i,st,0);for(int i = 1; i<=D; i++) add(F+2*n+i,ed,1),add(ed,F+2*n+i,0);for(int i = F+1; i<=F+n;i++) add(i,i+n,1),add(i+n,i,0);for(int numx,numy,i = 1; i<=n; i++) {scanf("%d%d",&numx,&numy);for(int x,j = 1; j<=numx; j++) scanf("%d",&x),add(x,F+i,1),add(F+i,x,0);for(int x,j = 1; j<=numy; j++) scanf("%d",&x),add(F+i+n,F+2*n+x,1),add(F+2*n+x,F+i+n,0);}printf("%d\n",dinic());return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439725.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机视觉那些事儿(1):基本任务

本文主要介绍深度学习在计算机视觉领域&#xff08;Computer vision&#xff09;基本任务中的应用&#xff0c;包括分类、检测、分割&#xff08;语义与实体)。 目录 引言 分类&#xff08;Classification&#xff09; 目标检测&#xff08;Object Detection&#xff09; T…

一步步编写操作系统 38 一级页表与虚拟地址3

接上&#xff0c;页是地址空间的计量单位&#xff0c;并不是专属物理地址或线性地址&#xff0c;只要是4KB的地址空间都可以称为一页&#xff0c;所以线性地址的一页也要对应物理地址的一页。一页大小为4KB&#xff0c;这样一来&#xff0c;4GB地址空间被划分成4GB/4KB1M个页&a…

《Python编程:从入门到实践》速查表

本文是Python畅销书《Python&#xff1a;从入门到实践》速查表。 随书配套视频观看地址&#xff1a;https://www.bilibili.com/video/av35698354 目录 1.Overview 2.Lists 3.Dictionaries 4.If and While Loops 5.Functions 6.Classes 7.Files and Exceptions 8.Testin…

【HDU - 5963】朋友(博弈,思维,必胜态必败态,找规律)

题干&#xff1a; B君在围观一群男生和一群女生玩游戏&#xff0c;具体来说游戏是这样的&#xff1a; 给出一棵n个节点的树&#xff0c;这棵树的每条边有一个权值&#xff0c;这个权值只可能是0或1。 在一局游戏开始时&#xff0c;会确定一个节点作为根。接下来从女生开始&…

一步步编写操作系统 39 二级页表1

前面讲述了页表的原理&#xff0c;并以一级页表做为原型讲述了地址转换过程。既然有了一级页表&#xff0c;为什么还要搞个二级页表呢&#xff1f;理由如下&#xff1a; 一级页表中最多可容纳1M&#xff08;1048576&#xff09;个页表项&#xff0c;每个页表项是4字节&#xf…

PointNet++详解与代码

在之前的一篇文章《PointNet&#xff1a;3D点集分类与分割深度学习模型》中分析了PointNet网络是如何进行3D点云数据分类与分割的。但是PointNet存在的一个缺点是无法获得局部特征&#xff0c;这使得它很难对复杂场景进行分析。在PointNet中&#xff0c;作者通过两个主要的方法…

一步步编写操作系统 40 内存分页下用户程序与操作系统的关系

分页的第一步要准备好一个页表&#xff0c;我们的页表是什么样子呢&#xff1f;现在我们要设计一个页表啦。 设计页表其实就是设计内存布局&#xff0c;不过在规划内存布局之前&#xff0c;我们需要了解用户进程与操作系统之间的关系。 前面讲保护模式时&#xff0c;我们知道…

【HDU - 4055】Number String(dp,思维)

题干&#xff1a; The signature of a permutation is a string that is computed as follows: for each pair of consecutive elements of the permutation, write down the letter I (increasing) if the second element is greater than the first one, otherwise write do…

一步步编写操作系统 41 快表tlb 简介

分页机制虽然很灵活&#xff0c;但您也看到了&#xff0c;为了实现虚拟地址到物理地址的映射&#xff0c;过程还是有些麻烦的。先要从CR3寄存器中获取页目录表物理地址&#xff0c;然后用虚拟地址的高10位乘以4的积做为在页目录表中的偏移量去寻址目录项pde&#xff0c;从pde中…

【CodeForces - 731C】Socks(并查集,思维)

题干&#xff1a; Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniys clothes. Ten minutes before her leave she real…

50个最有用的Matplotlib数据分析与可视化图

本文介绍了数据分析与可视化中最有用的50个数据分析图&#xff0c;共分为7大类&#xff1a;Correlation、Deviation、RankIng、Distribution、Composition、Change、Groups 原文链接&#xff1a;https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-t…

一步步编写操作系统 42 用c语言编写内核

在这之前&#xff0c;我们一直用汇编语言直接与机器对话&#xff0c;如果大家不知道这个世界上有高级语言的话&#xff0c;我想大家也不会觉得写汇编代码的过程很辛苦&#xff0c;哈哈&#xff0c;幸福确实是比较出来的。相对于汇编语言&#xff0c;用c 语言写内核是非常爽的事…

【CodeForces - 722C】Destroying Array(并查集,时光倒流)

题干&#xff1a; 给定一个有n个数的序列a1,a2, ..., an 你每次可以将序列中一个数删去&#xff0c;剩下的数会被分割为一段一段连续的数列 给定一个删除数的顺序&#xff0c;问在每次删除之后&#xff0c;剩下的连续的数列中&#xff0c;数列和的最大值为多少 Input 第…

视觉SLAM十四讲(1):预备知识

最近在学习高翔博士的《视觉SLAM十四讲》&#xff08;第二版&#xff09;&#xff0c;算是初学本书&#xff0c;配套资源还算蛮丰富的&#xff0c;有代码&#xff08;第一版和第二版都有&#xff09;&#xff0c;B站上也有高翔博士对第一版录制的讲解视频&#xff0c;真的是很贴…

一步步编写操作系统 43 汇编语言和c语言的理解

也许有的同学喜欢用汇编语言来实现操作系统&#xff0c;觉得用汇编来写程序似乎更简单直接&#xff0c;可控性比较强&#xff0c;有种“一切尽在掌握”的赶脚。而用c语言实现操作系统这件事&#xff0c;虽然轻松很多&#xff0c;但似乎隐约感觉到有些慌张。因为虽然c语言相对来…

视觉SLAM十四讲(2):初识SLAM

这一讲主要介绍视觉SLAM的结构&#xff0c;并完成第一个SLAM程序&#xff1a;HelloSLAM。 目录 2.1 小萝卜的例子 单目相机 双目相机 深度相机 2.2 经典视觉SLAM框架 2.3 SLAM问题的数学表述 2.4 编程实践 Hello SLAM 使用cmake 使用库 【高翔】视觉SLAM十四讲2.1 小…

【CodeForces - 467C】George and Job(dp,思维)

题干&#xff1a; The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didnt have enough money, so George was going to work as a programmer. Now he faced the following problem at the work. Given a sequence of …

一步步编写操作系统 44 用c语言编写内核1

先来个简单的&#xff0c;欢迎我们神秘嘉宾——main.c。这是我们第一个c语言代码。 1 int main(void) { 2 while(1); 3 return 0; 4 }它没法再简单啦&#xff0c;简单的程序似乎能帮助咱们更容易的理解所学的知识&#xff0c;哈哈&#xff0c;我说的是似乎&#xff0c;其实&…

从零实现一个3D目标检测算法(1):3D目标检测概述

本文是根据github上的开源项目&#xff1a;https://github.com/open-mmlab/OpenPCDet整理而来&#xff0c;在此表示感谢&#xff0c;强烈推荐大家去关注。使用的预训练模型也为此项目中提供的模型&#xff0c;不过此项目已更新为v0.2版&#xff0c;与本文中代码略有不同。 本文…

【Codeforces - 900C】Remove Extra One(思维,STLset,tricks)

题干&#xff1a; You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a1, a2, ..., ak the element ai is a record if for every integer…