Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20052   Accepted: 8915

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

  1. 4 3 3
  2. 2 2 1 2 3 1
  3. 2 2 2 3 1 2
  4. 2 2 1 3 1 2
  5. 2 1 1 3 3

Sample Output

  1. 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.

Source

分析

拆点+网络流最大流

code

  1. #include<cstdio>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const int INF = 1e9;
  7. const int N = ;
  8.  
  9. struct Edge{
  10. int to,nxt,c;
  11. }e[N];
  12. int head[N],dis[N],cur[N],q[];
  13. int tot = ,L,R,S,T;
  14.  
  15. inline char nc() {
  16. static char buf[],*p1 = buf,*p2 = buf;
  17. return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
  18. }
  19. inline int read() {
  20. int x = ,f = ;char ch = nc();
  21. for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
  22. for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
  23. return x * f;
  24. }
  25. inline void add_edge(int u,int v,int w) {
  26. e[++tot].to = v,e[tot].c = w,e[tot].nxt = head[u],head[u] = tot;
  27. e[++tot].to = u,e[tot].c = ,e[tot].nxt = head[v],head[v] = tot;
  28. }
  29. bool bfs() {
  30. for (int i=; i<=T; ++i) {
  31. cur[i] = head[i];dis[i] = -;
  32. }
  33. L = ;R = ;
  34. q[++R] = S;dis[S] = ;
  35. while (L <= R) {
  36. int u = q[L++];
  37. for (int i=head[u]; i; i=e[i].nxt) {
  38. int v = e[i].to,c = e[i].c;
  39. if (dis[v]==- && c>) {
  40. dis[v] = dis[u] + ;
  41. q[++R] = v;
  42. if (v==T) return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. int dfs(int u,int flow) {
  49. if (u==T) return flow;
  50. int used = ;
  51. for (int &i=cur[u]; i; i=e[i].nxt) {
  52. int v = e[i].to,c = e[i].c;
  53. if (dis[v]==dis[u]+ && c>) {
  54. int tmp = dfs(v,min(c,flow-used));
  55. if (tmp > ) {
  56. e[i].c -= tmp;e[i^].c += tmp;
  57. used += tmp;
  58. if (used==flow) break;
  59. }
  60. }
  61. }
  62. if (used!=flow) dis[u] = -;
  63. return used;
  64. }
  65. inline int dinic() {
  66. int ans = ;
  67. while (bfs()) ans += dfs(S,INF);
  68. return ans;
  69. }
  70. int main() {
  71. int n = read(),F = read(),D = read();
  72. S = ,T = n+n+F+D+;
  73. for (int i=; i<=n; ++i) {
  74. int f = read(),d = read();
  75. for (int a,j=; j<=f; ++j)
  76. a = read(),add_edge(a+n+n,i,);
  77. for (int a,j=; j<=d; ++j)
  78. a = read(),add_edge(i+n,a+n+n+F,);
  79. }
  80. for (int i=; i<=n; ++i) add_edge(i,i+n,);
  81. for (int i=; i<=F; ++i) add_edge(S,i+n+n,);
  82. for (int i=; i<=D; ++i) add_edge(i+n+n+F,T,);
  83. printf("%d",dinic());
  84. return ;
  85. }

poj 3281 Dining(网络流+拆点)的更多相关文章

  1. POJ 3281 Dining(网络流-拆点)

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  2. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  3. POJ - 3281 Dining(拆点+最大网络流)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18230   Accepted: 8132 Descripti ...

  4. poj 3281 Dining【拆点网络流】

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11828   Accepted: 5437 Descripti ...

  5. POJ 3281 Dining 网络流最大流

    B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  6. POJ 3281 Dining (网络流之最大流)

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...

  7. POJ 3281 Dining[网络流]

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  8. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...

  9. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  10. POJ 3281 Dining(最大流)

    POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...

随机推荐

  1. 关于C#解析shp文件

    最近在做项目时,要求可以上传shp文件到指定的地图中,地图开发使用的arcgisapi,网上找了好多解析shp文件的js,但都不是太理想,直到群里的小伙伴提到Gdal 首先,到GDAL官网下载自己使用 ...

  2. tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091

    1.tomcat6大并发出现:INFO: Maximum number of threads (200) created for connector with address null and por ...

  3. Quartz.NET基础入门

    Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和 ...

  4. Java面向对象(构造方法、this、super)

    面向对象 今日内容介绍 u 构造方法 u this u super 第1章 构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被pr ...

  5. MFC制作简单通讯录程序

    学习c++和MFC一段时间了,苦于没有项目实战,所以自己写了一个简单的简单通讯录程序,以前用c#写简单很多,例程是这本书上的实例,我的第一个winform程序也是从这本书上学的,总结c#写的话更简单, ...

  6. freebsd问题

    http://community.spiceworks.com/topic/91708-server-freezes

  7. 一键部署LNMP堆栈Web应用基础架构

    https://market.azure.cn/Vhd/Show?vhdId=9852&version=10884 产品详情 产品介绍LEMP/LNMP 是指一组通常一起使用来运行动态网站或者 ...

  8. 洛谷 P2691 逃离

    题目描述 一个n×n栅格是由n行和n列顶点组成的一个无向图,如图所示.用(i,j)表示处于第i行第j列的顶点.除了边界顶点(即满足i=1,i=n,j=1或j=n的顶点(i,j)),栅格中的所有其他顶点 ...

  9. 2017-3-7-lint183-wood-cut

    2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...

  10. ASUS主板 Type C 接口无效问题

    修改UEFI设置,把 USB TYPE C POWER SWITCH 改成启用