解题报告 之 POJ3281 Dining

Description

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  Fi integers 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.

题目大意:有n头牛,f种草料,d种水,每种草料和每种水仅有一个单位,每头牛要吃一个单位草料和喝一个单位水。每头牛只对一部分草料和一部分水感兴趣。问你最多能完全满足多少头牛(草料和水均满意)?

分析:经典构图题:用拆点来限制流量,超级源点与每种食物相连,负载为1。每头牛拆点,负载为1(此处限制流量,一头牛只能转移一的流量)。每头牛拆点的出点与所有满意的食物相连,负载为1;每头牛拆点的入点再与所有满意的水相连,负载为1,每种水再与超级汇点相连,负载为1。最终看最大流跑出多少就是最大的,能够双双满足的牛的数量。

图解:

(另外有一种变体是食物和饮料数量不是1,则相应改变超级源点和超级汇点连接的边为对应数量即可。)

上代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;const int MAXM = 251000;
const int MAXN = 510;
const int INF = 0x3f3f3f3f;struct Edge
{int to, cap, next;
};Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;void addedge( int from, int to, int cap )
{edge[cnt].to = to;edge[cnt].cap = cap;edge[cnt].next = head[from];head[from] = cnt++;swap( from, to );edge[cnt].to = to;edge[cnt].cap = 0;edge[cnt].next = head[from];head[from] = cnt++;
}int bfs()
{memset( level, -1, sizeof level );queue<int>q;while (!q.empty())q.pop();level[src] = 0;q.push( src );while (!q.empty()){int u = q.front();q.pop();for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (edge[i].cap > 0 && level[v] == -1){level[v] = level[u] + 1;q.push( v );}}}return level[des] != -1;
}int dfs( int u, int f )
{if (u == des) return f;int tem;for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (edge[i].cap>0&&level[v] == level[u] + 1){tem = dfs( v, min( f, edge[i].cap ) );if (tem > 0){edge[i].cap -= tem;edge[i^1].cap += tem;return tem;}}}level[u] = -1;return 0;
}int Dinic()
{int ans = 0, tem;while (bfs()){while ((tem = dfs( src, INF )) > 0){ans += tem;}}return ans;
}int main()
{int n, f, d;src = 0;des = 505;while (cin >> n>>f>>d){memset( head, -1, sizeof head );cnt = 0;for (int i = 1; i <= f; i++)addedge( src, i + 200, 1 );for (int i = 1; i <= d; i++)addedge( i + 300, des, 1 );for (int i = 1; i <= n; i++){addedge( i, i + 100, 1 );int F, D;cin >> F>>D;for (int j = 1; j <= F; j++){int tem;cin >> tem;addedge( tem + 200, i, 1 );}for (int j = 1; j <= D; j++){int tem;cin >> tem;addedge( i+100, tem + 300, 1 );}}cout << Dinic() << endl;}return 0;}

啦啦啦,通信原理课果然很无聊。。。他都不知道我们听不懂。。

解题报告 之 POJ3281 Dining相关推荐

  1. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  2. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  3. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  4. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  5. 解题报告(十三)中国剩余定理(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  6. 解题报告(四)生成函数(ACM/ OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  7. 解题报告(八) prufer 序列与 Cayley 公式(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  8. 解题报告(一)E、(BZOJ4589)Hard Nim(博弈论 + FWT)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  9. 解题报告(五)组合计数(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

最新文章

  1. 浅谈数据分析的魅力和能力要求!
  2. 湖南卫视新年巨献敲定 《恋爱兵法》显偶像魅力
  3. STM32 SPI的使用
  4. 爬虫python漏洞群_如何用爬虫获取cnvd漏洞库?
  5. svn 常用操作命令
  6. JVM内存区域(运行时数据区)划分
  7. (stack栈)rails
  8. wkhtmltopdf 水印 背景_wkhtmltopdf + echarts 转 PDF
  9. 【数据库】第一章 数据库的分类、SQL、数据库、表和表记录的常用操作
  10. 网地址和广播地址的计算
  11. 【组合逻辑电路】——通用译码器
  12. 计算机网络的三个基本拓扑结构类型,计算机网络拓扑结构的分类
  13. 关于企业数字化转型的建议
  14. 视频格式720P、1080i 和 1080P
  15. demonstration记忆_记忆单词的方法
  16. Android 手机安装有微信,还是提示未安装微信
  17. 【Lingo】分段函数
  18. 【01】 Nastran 生成adams接口模态中性文件(mnf文件)
  19. kbd通达2017版破解后,自定义菜单无法调用表单开启流程/kbdkbd 错误,请联系管理员 /general/approve_center/new/insert.php/kbd
  20. win10 卸载cuda

热门文章

  1. 令人眼花缭乱的“军刀”牌背包
  2. oracle数据库升级失败怎么办,【案例】Oracle报错ORA-00918 数据库升级后遇到SQL BUG 5368296...
  3. Zakra WordPress多功能主题 免费博客主题
  4. Android4.2.2 ViVo Xplay510W 2.16.3/最新官方固件/完美root/完美支持OTA/状态栏流量显示/稳定省电ROM
  5. 红外遥控器快速编码解码(NEC)
  6. 关于x86_64和x32和x86和-386和32位还是64位的区分 指令集的学习
  7. 天融信 服务器映射,天融信防火墙端口映射問題
  8. 2021.5.10笔记 内部类
  9. 《京韵大鼓——俞伯牙摔琴》(骆玉笙)(唱词文本)
  10. 电子相册视频制作工具:photo2movie for Mac