POJ 3281 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: 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.

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

Http

POJ:https://vjudge.net/problem/POJ-3281

Source

网络流

题目大意

有n只牛,F种食物,D种饮料,每一只牛喜欢若干食物和饮料。现在要给每一只牛分配食物和饮料,每一种食物或饮料只够一只牛吃。若一只牛吃到了它喜欢的食物和饮料(注意是两个都要符合),它就会很开心。现在求最多能使多少只牛开心

解决思路

这道题与Luogu1402有些类似,但不知道为什么我用二分图的方法会WrongAnswer。所以我们用网络流最大流来解决。

对于每一只牛i我们把其拆成两个点i和i+n,在i与i+n中连一条流量为1的边。这样是为了保证一只牛只吃到一种食物和一种饮料。再在对应的食物与牛i连容量为1的边,在对应的牛与饮料连容量为1的边。最后再连上超级源点和超级汇点就可以了。

虽然笔者是使用EK算法实现的网络流最大流,但是更推荐效率更优的Dinic算法,具体请移步我的这篇文章

代码

EK算法

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. const int maxN=510;
  9. const int maxM=2147483647;
  10. const int inf=2147483647;
  11. int n,F,D;
  12. int G[maxN][maxN];
  13. int Flow[maxN];
  14. int Path[maxN];
  15. bool bfs();
  16. int main()
  17. {
  18. memset(G,0,sizeof(G));
  19. scanf("%d%d%d",&n,&F,&D);
  20. for (int i=1;i<=n;i++)//先在拆出来的牛点之间连边
  21. G[i][i+n]=1;
  22. for (int i=1;i<=n;i++)
  23. {
  24. int n1,n2;
  25. scanf("%d%d",&n1,&n2);
  26. for (int j=1;j<=n1;j++)
  27. {
  28. int v;
  29. scanf("%d",&v);
  30. G[2*n+v][i]=1;//连牛与食物
  31. }
  32. for (int j=1;j<=n2;j++)
  33. {
  34. int v;
  35. scanf("%d",&v);
  36. G[i+n][2*n+F+v]=1;//连牛与饮料
  37. }
  38. }
  39. for (int i=1;i<=F;i++)
  40. G[0][n*2+i]=1;//连源点
  41. for (int i=1;i<=D;i++)
  42. G[n*2+F+i][n*2+F+D+1]=1;//连汇点
  43. int Ans=0;
  44. while (bfs())//EK算法
  45. {
  46. int di=Flow[n*2+F+D+1];
  47. int now=n*2+F+D+1;
  48. int last=Path[now];
  49. while (now!=0)
  50. {
  51. G[last][now]-=di;
  52. G[now][last]+=di;
  53. now=last;
  54. last=Path[now];
  55. }
  56. Ans++;
  57. }
  58. cout<<Ans<<endl;
  59. return 0;
  60. }
  61. bool bfs()
  62. {
  63. memset(Path,-1,sizeof(Path));
  64. memset(Flow,0,sizeof(Flow));
  65. Flow[0]=inf;
  66. queue<int> Q;
  67. while (!Q.empty())
  68. Q.pop();
  69. Q.push(0);
  70. do
  71. {
  72. int u=Q.front();
  73. Q.pop();
  74. for (int i=0;i<=2*n+F+D+1;i++)
  75. {
  76. if ((Path[i]==-1)&&(G[u][i]>0))
  77. {
  78. Path[i]=u;
  79. Q.push(i);
  80. Flow[i]=min(Flow[u],G[u][i]);
  81. }
  82. }
  83. }
  84. while (!Q.empty());
  85. if (Flow[n*2+F+D+1]==0)
  86. return 0;
  87. return 1;
  88. }

POJ 3281 Dining (网络流)的更多相关文章

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

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

  2. POJ 3281 Dining 网络流最大流

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

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

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

  4. POJ 3281 Dining[网络流]

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

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

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

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

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

  7. POJ 3281 Dining(最大流)

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

  8. POJ 3281 Dining(网络流拆点)

    [题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...

  9. poj 3281 Dining(网络流+拆点)

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

随机推荐

  1. 20155207 《网络对抗》exp4 恶意代码分析 学习总结

    20155207 <网络对抗> 恶意代码分析 学习总结 实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件 ...

  2. 20155330 《网络对抗》 Exp8 Web基础

    20155330 <网络对抗> Exp8 Web基础 实验问题回答 什么是表单 表单可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一个表单有三个基本组成部分 表单标签 ...

  3. POJ1035&&POJ3080&&POJ1936

    字符串处理专题,很早就写好了然而忘记写blog了 1035 题意:给你一些单词作为字典.然后让你查找一些单词.对于每个单词,如果在字典中就输出它.否则输出所有它通过删除||增加||替换一个字符能得到的 ...

  4. 如何查看哪个进程,使用了哪个CPU

    某些时候,我们需要知道,在Unix/Linux 环境中,CPU究竟消耗在了哪些进程上面. 如下是最简单的方法: ps -elF

  5. mfc 动态创建EDIT控件

    知识点: CWnd::Create CWnd::CreateEx Spy++工具 动态创建控件 一. CWnd::Create 参数 virtual BOOL Create( LPCTSTR lpsz ...

  6. MySQL主从报错1594

    一.主从报错 Relay log read failure 问题原因,MySQL主从使用的是kvm虚拟机,物理机超分严重,在负载高的情况下会kill掉占用资源最多的虚拟机,再启动后导致主从失败 mys ...

  7. asp.net mvc2+nhibernate实体类映射问题之“尝试创建Controller类型的控制器时出错请确保控制器具有无参数公共构造函数”

    程序出了问题,解决后发现如此简单,犯的错误是如此的低级啊,特此记录! 运行程序总是在浏览器中看到一片空白,什么也没有,用application_error跟踪发现抓出一个这样的异常 然后浏览器中就是这 ...

  8. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

  9. php faker 库填充数据

    Generating Fake Data in PHP with Faker 时间 2016-01-28 07:13:00 Wern Ancheta 原文  http://wern-ancheta.c ...

  10. 吉他软件Guitar Pro播放无声音的解决方法

    系统环境:适用于Windows操作系统和macOS的Guitar Pro . 问题表现:已成功安装Guitar Pro ,但用Guitar Pro 播放时没有任何声音或播放失真. 解决方案:首先确保安 ...