题干:

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;
}

【POJ - 3281】Dining(拆点建图,网络流最大流)相关推荐

  1. POJ.3281 dining 最大流+拆点

    POJ.3281 dining 最大流+拆点 思路清晰为啥一直WA呢 #include <iostream> #include <cstring> #include <v ...

  2. POJ 3281 -- Dining(最大流,拆点建图)

    题目链接 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drink ...

  3. 解题报告:POJ 3281 Dining(最大流 / “三分图”建图)

    B.POJ 3281 DiningDiningDining(最大流/建图模板)[省选/NOI- ] 有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一 种食物和一种饮料.现 ...

  4. POJ - 3281 Dining(最大流+思维建边)

    题目链接:点击查看 题目大意:给出n头奶牛,f种食物,d种饮料,每只奶牛可以选择数种食物和饮料,但每种食物和饮料只有一份,现在问最多能让多少头奶牛同时满足食物和饮料的条件 题目分析:最大流的题目,不过 ...

  5. 2016 计蒜之道 复赛 菜鸟物流的运输网络【拆点+思维建图+网络流】

    菜鸟物流的运输网络 菜鸟物流有自己的运输网络,网络中包含 nn 个城市物流集散中心,和 mm 对城市之间的运输线路(线路是双向的).菜鸟物流允许淘宝卖家自行确定包裹的运输路径,但只有一条限制规则:不允 ...

  6. [ZJOI2010] 贪吃的老鼠(二分+差分+神仙建图网络流)

    problem luogu-P2570 solution 卧槽网络流尼玛神题 首先这个最小延长时间 TTT ,套路地考虑二分,将问题转化为判定性问题. 其次 n,mn,mn,m 和奶酪存在时间 [l, ...

  7. POJ - 1637 Sightseeing tour(混合图欧拉回路的求解--建图跑最大流)

    题目链接:https://vjudge.net/contest/399194#problem/B The city executive board in Lund wants to construct ...

  8. POJ ~ 2502 ~ Subway (Dijkstra + 建图)

    题意:你要从家去学校,先输入你家和学校的坐标.有一些地铁站线,每一条线上有一些站点,每一条线以一对-1,-1结束,地铁站的输入以EOF结束.坐标单位为米,你行走速度为10km/h,地铁速度为40km/ ...

  9. hdu4560 不错的建图,二分最大流

    题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...

最新文章

  1. HDU 6229 Wandering Robots 找规律+离散化
  2. Java注解和xml_Spring注解配置和xml配置优缺点比较
  3. Shell命令-管理与性能监视之strace、ltrace
  4. 在当当买了python怎么下载源代码-Python爬取当当网APP数据
  5. python3.7如何使用enum_Python3.4 枚举类型的使用
  6. 面向串口编程java_Java串口编程例子
  7. Delphi之Exception获得错误信息
  8. yum离线下载rpm包
  9. Openstack(十四)创建虚拟机
  10. DP算法——最大子序列系列Ⅰ
  11. ESP32创建局域网服务器VScode
  12. bitbucket配置_如何配置Bitbucket的ssh
  13. 最新的计算机主板,最新主流电脑主板天梯图2020
  14. JAVA中输出分两栏,老司机搞定Java 设置Word分栏
  15. 饥荒linux服务器搭建
  16. 服务器验收性能标准,云服务器 验收
  17. matlab雷达目标回波仿真
  18. commvault备份mysql数据库_Oracle数据库的备份和恢复-Commvault.PDF
  19. c++ 取整之ceil、floor、round、fix用法
  20. rz: xxxxxxx removed

热门文章

  1. 树的直径,树的最长路dp思想
  2. 使用Combres 库 ASP.NET 网站优化
  3. [Leetcode][第336题][JAVA][回文对][暴力][HashSet][字典树]
  4. [leedcode][JAVA][365][BFS]
  5. Hihocoder 1632 : Secret Poems 思维|技巧
  6. 生产系统服务器是啥意思,生产系统服务器主机名怎么看
  7. python全栈工程师能接到私活么_Python全栈工程师(包、模块 的导入)
  8. python词频统计结果写入csv_Python词频对比并导入CSV文件
  9. mongodb如何根据字段(数组类型)的长度排序_大数据存储技术选型(七)——MongoDB设计模式及索引优化...
  10. 群晖218 修改服务器名称,一次换群晖引发的各种事情——论如何榨干218+的价值【不完全版】...