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

确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <stack>
  10. #include <set>
  11. #include <map>
  12. #define INF 0x3f3f3f3f
  13. #define lowbit(x) (x&(-x))
  14. #define eps 0.00000001
  15. #define pn printf("\n")
  16. using namespace std;
  17. typedef long long ll;
  18.  
  19. int n,f,d;
  20. const int MAXN = ;
  21. const int MAXM = ;
  22. struct Edge
  23. {
  24. int to,next,cap,flow;
  25. }edge[MAXM];
  26. set <pair<int,int> > st;
  27. int tol;
  28. int head[MAXN];
  29. int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
  30. {
  31. tol = ;
  32. memset(head,-,sizeof(head));
  33. }
  34. //加边,单向图三个参数,双向图四个参数
  35. void addedge(int u,int v,int w,int rw=) {
  36. edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
  37. edge[tol].flow = ;head[u] = tol++;
  38. edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
  39. edge[tol].flow = ;head[v]=tol++;
  40. }
  41.  
  42. int sap(int start,int end,int N) {
  43. memset(gap,,sizeof(gap));
  44. memset(dep,,sizeof(dep));
  45. memcpy(cur,head,sizeof(head));
  46. int u = start;
  47. pre[u] = -;
  48. gap[] = N;
  49. int ans = ;
  50. while(dep[start] < N)
  51. {
  52. if(u == end)
  53. {
  54. int Min = INF;
  55. for(int i = pre[u];i != -; i = pre[edge[i^].to])
  56. if(Min > edge[i].cap - edge[i].flow)
  57. Min = edge[i].cap - edge[i].flow;
  58. for(int i = pre[u];i != -; i = pre[edge[i^].to])
  59. {
  60. edge[i].flow += Min;
  61. edge[i^].flow -= Min;
  62. }
  63. u = start;
  64. ans += Min;
  65. continue;
  66. }
  67. bool flag = false;
  68. int v;
  69. for(int i = cur[u]; i != -;i = edge[i].next)
  70. {
  71. v = edge[i].to;
  72. if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
  73. {
  74. flag = true;
  75. cur[u] = pre[v] = i; break;
  76. }
  77. }
  78. if(flag)
  79. {
  80. u = v;
  81. continue;
  82. }
  83. int Min = N;
  84. for(int i = head[u]; i != -;i = edge[i].next)
  85. if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
  86. {
  87. Min = dep[edge[i].to];
  88. cur[u] = i;
  89. }
  90. gap[dep[u]]--;
  91. if(!gap[dep[u]])return ans;
  92. dep[u] = Min+;
  93. gap[dep[u]]++;
  94. if(u != start) u = edge[pre[u]^].to;
  95. }
  96. return ans;
  97. }
  98. int main()
  99. {
  100. init();
  101. int fi, di, x;
  102. cin >> n >> f >> d;
  103. for(int i=;i<=f;i++) addedge(, *n+i, );
  104. for(int i=;i<=d;i++) addedge(*n+f+i, *n+f+d+, );
  105. for(int i=;i<=n;i++) addedge(i, n+i, );
  106. for(int i=;i<=n;i++)
  107. {
  108. scanf("%d%d",&fi,&di);
  109. for(int j=;j<fi;j++)
  110. {
  111. scanf("%d",&x);
  112. addedge(*n+x, i, );
  113. }
  114. for(int j=;j<di;j++)
  115. {
  116. scanf("%d",&x);
  117. addedge(i+n,*n+f+x,);
  118. }
  119. }
  120. cout << sap(, *n+f+d+, *n+f+d+) << endl;
  121. }

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 (网络流构图)

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

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

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

  6. POJ 3281 Dining (网络流)

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

  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. Tensorflow 0.8.0 安装配置方法

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51280087 折腾了一下,给工作站配置 ...

  2. Java上使用Lombok插件简化Getter、Setter方法

    Maven引入依赖: <dependencies> <dependency> <groupId>org.projectlombok</groupId> ...

  3. RestTemplate使用详解

    1.RestTemplate添加RequestHeader如content-type可通过httpclient设置 List<Header> headers = new ArrayList ...

  4. Android-黑科技-微信抢红包必备软件

                        黑科技微信抢红包 介绍: 本节类容和技术无太大关系.主要是个人认为比較好玩,年关将至,对于新起之秀微信红包.绝对是过春节首选.看到就是赚到,速速围观下载.眼下仅 ...

  5. 基于ArcGIS Flex API实现动态标绘(1.0)

    标绘作为一种数据展示形式,在多个行业都有需求. 基于ArcGIS Flex API(3.6)实现标绘API,当前版本号1.0 alpha,支持经常使用几种标绘符号,包含: 圆弧.曲线.圆形.椭圆.弓形 ...

  6. hdu 1002 A + B Problem II(大正整数相加)

    代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using names ...

  7. libcanbus官方主页

    libcanbus canbus(CAN BUS V2.0 B)扩展格式库项目简析 注: 本文如果你已经有linux开发环境 请确保你使用本库时是tag版本号. 该库遵循的协议是SAE J1939-2 ...

  8. luogu2441 角色属性树

    题目大意:维护一个可查询.修改的树,查询的是一个节点的:离它距离最近的.组成两个节点Key值的质因数存在交集的.祖先节点:修改是修改一个节点的key值. 如果组成两个Key值的质因数存在交集,则两个数 ...

  9. Windows 平台下 Go 语言的安装和环境变量设置

    1. Go 语言 SDK 安装包下载和安装 最新稳定版 1.5.3 安装包 go1.5.3.windows-amd64.msi下载地址 https://golang.org/dl/,大小约 69 MB ...

  10. ubuntu终端白屏的解决方法

    昨天突发奇想的想为teminal设置一个背景, 这样.... 不过过了一会就高兴不起来了,,,,终端白屏!好吧,现在我页没办法彻底解决, 不过暂时的一个方法是可以把首选项->背景->背景图 ...