题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,

而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?

析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点,

一个和食物连,一个和饮料连,最后跑一遍最大流即可。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const double inf = 0x3f3f3f3f3f3f;
  30. const double PI = acos(-1.0);
  31. const double eps = 1e-8;
  32. const int maxn = 400 + 5;
  33. const int mod = 1e9;
  34. const int dr[] = {-1, 0, 1, 0};
  35. const int dc[] = {0, 1, 0, -1};
  36. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  37. int n, m;
  38. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  39. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. inline bool is_in(int r, int c){
  41. return r >= 0 && r < n && c >= 0 && c < m;
  42. }
  43. struct Edge{
  44. int from, to, cap, flow;
  45. };
  46.  
  47. struct Dinic{
  48. int n, m, s, t;
  49. vector<Edge> edges;
  50. vector<int> G[maxn];
  51. bool vis[maxn];
  52. int d[maxn];
  53. int cur[maxn];
  54.  
  55. void init(){
  56. edges.clear();
  57. for(int i = 0; i < maxn; ++i) G[i].clear();
  58. }
  59.  
  60. void addEdge(int from, int to, int cap){
  61. edges.push_back((Edge){from, to, cap, 0});
  62. edges.push_back((Edge){to, from, 0, 0});
  63. m = edges.size();
  64. G[from].push_back(m-2);
  65. G[to].push_back(m-1);
  66. }
  67.  
  68. bool bfs(){
  69. memset(vis, 0, sizeof vis);
  70. queue<int> q;
  71. q.push(s);
  72. d[s] = 0;
  73. vis[s] = 1;
  74. while(!q.empty()){
  75. int x = q.front(); q.pop();
  76. for(int i = 0; i < G[x].size(); ++i){
  77. Edge &e = edges[G[x][i]];
  78. if(!vis[e.to] && e.cap > e.flow){
  79. vis[e.to] = 1;
  80. d[e.to] = d[x] + 1;
  81. q.push(e.to);
  82. }
  83. }
  84. }
  85. return vis[t];
  86. }
  87.  
  88. int dfs(int x, int a){
  89. if(x == t || a == 0) return a;
  90. int flow = 0, f;
  91. for(int &i = cur[x]; i < G[x].size(); ++i){
  92. Edge &e = edges[G[x][i]];
  93. if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
  94. e.flow += f;
  95. edges[G[x][i]^1].flow -= f;
  96. flow += f;
  97. a -= f;
  98. if(a == 0) break;
  99. }
  100. }
  101. return flow;
  102. }
  103.  
  104. int maxFlow(int s, int t){
  105. this->s = s; this->t = t;
  106. int flow = 0;
  107. while(bfs()){
  108. memset(cur, 0, sizeof cur);
  109. flow += dfs(s, INF);
  110. }
  111. return flow;
  112. }
  113. };
  114. Dinic dinic;
  115.  
  116. int main(){
  117. int k;
  118. while(scanf("%d %d %d", &n, &m, &k) == 3){
  119. dinic.init();
  120. int s = 0, t = 402;
  121. for(int i = 1; i <= m; ++i) dinic.addEdge(s, i+200, 1);
  122. for(int i = 1; i <= k; ++i) dinic.addEdge(i+300, t, 1);
  123. for(int i = 1; i <= n; ++i){
  124. int f, d;
  125. dinic.addEdge(i, i+100, 1);
  126. scanf("%d %d", &f, &d);
  127. for(int j = 0; j < f; ++j){
  128. int x;
  129. scanf("%d", &x);
  130. dinic.addEdge(x+200, i, 1);
  131. }
  132. for(int j = 0; j < d; ++j){
  133. int x;
  134. scanf("%d", &x);
  135. dinic.addEdge(i+100, x+300, 1);
  136. }
  137. }
  138. printf("%d\n", dinic.maxFlow(s, t));
  139. }
  140. return 0;
  141. }

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

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

  4. poj 3281 Dining 拆点 最大流

    题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...

  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(网络流-拆点)

    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 (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  8. POJ 3281 Dining(最大流)

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

  9. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

随机推荐

  1. 基于传统IPC基础上的RTMP互联网推流摄像机方案设计

    在我之前的一篇博客<EasyRTMP内置进入摄像机中实现网络推流直播摄像机的功能>中,我阐述了一种将RTMP推流内置到摄像机系统内部,实现安防摄像机转互联网直播的RTMP推流摄像机功能,如 ...

  2. 常见 WEB 安全漏洞(转)

    SQL注入 成因:程序未对用户的输入的内容进行过滤,从而直接代入数据库查询,所以导致了sql 注入 漏洞 . 思路:在URL处可以通过 单引号 和 and 1=1 and 1=2 等语句进行手工测试s ...

  3. 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...

  4. Django框架ORM单表删除表记录_模型层

    此方法依赖的表是之前创建的过的一张表 参考链接:https://www.cnblogs.com/apollo1616/p/9840354.html 1.删除方法就是delete(),它运行时立即删除对 ...

  5. [2018-10-17]宁波dotnet社区(NBDNC)第一次问卷关于dotnet技术栈的小调查

    最近(2018年10月7日至10月17日),为配合确定下一次社区线下活动主题,做了一次宁波dotnet社区(NBDNC)的本地dotnet技术栈调研,设计了一份问卷,在此做一次记录. 导出的问卷统计结 ...

  6. Django--组件-用户认证Auth(auth_user增加字段)

    引入 :  from django.db import models from django.contrib.auth.models import AbstractBaseUser 源码 :  fro ...

  7. ZOJ - 1504 Slots of Fun 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1504 题意 给出一串字符串 里面的每个字符的位置 都按照题目的意 ...

  8. linux 下监控进程流量情况命令 NetHogs

    摘自: http://www.cnblogs.com/kerrycode/p/4748970.html NetHogs介绍 NetHogs是一款开源.免费的,终端下的网络流量监控工具,它可监控Linu ...

  9. 关于for 循环里 线程执行顺序问题

    最近在做项目时遇到了 这样的需求 要在一个for循环里执行下载的操作, 而且要等 下载完每个 再去接着走循环.上网查了一些 觉得说的不是很明确.现在把我用到的代码 贴上 希望可以帮到有此需求的开发者  ...

  10. poj1417 True Liars[并查集+背包]

    有一点小转化的题,在设计dp状态时还是有点费脑筋的. 地址. 依题意,首先可以知道肯定要扩展域的并查集(明摆着的嘛).一个"好人"域,一个"坏人"域,每句话分两 ...