题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292

You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible.
  The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.
  You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.
  Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.

题意描述:你准备了F种食物(F<=200)和D种饮料(D<=200),每种食物和饮料都有一定的数量。有n个人(n<=200),每个人都有喜欢的食物和饮料并且只能吃自己喜欢的食物、喝喜欢的饮料,求出能够吃到喜欢的食物并喝到喜欢的饮料的最大人数。

算法分析:运用网络最大流求解即可。新增源点from和汇点to,from->食物(w为食物数量),饮料->to(w为饮料数量),对每个人 i 拆边 i 和 i+n ,喜欢的食物->i(w为1),i -> i+n(w为1),i+n->喜欢的饮料(w为1)。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<vector>
  8 #include<queue>
  9 #define inf 0x7fffffff
 10 using namespace std;
 11 const int maxn=200+10;
 12 const int M = 999999;
 13
 14 int n,F,D;
 15 int from,to,d[maxn*10];
 16 struct node
 17 {
 18     int v,flow;
 19     int next;
 20 }edge[M*2];
 21 int head[maxn*10],edgenum;
 22
 23 void add(int u,int v,int flow)
 24 {
 25     edge[edgenum].v=v ;edge[edgenum].flow=flow;
 26     edge[edgenum].next=head[u];
 27     head[u]=edgenum++;
 28
 29     edge[edgenum].v=u ;edge[edgenum].flow=0;
 30     edge[edgenum].next=head[v];
 31     head[v]=edgenum++;
 32 }
 33
 34 int bfs()
 35 {
 36     memset(d,0,sizeof(d));
 37     d[from]=1;
 38     queue<int> Q;
 39     Q.push(from);
 40     while (!Q.empty())
 41     {
 42         int u=Q.front() ;Q.pop() ;
 43         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 44         {
 45             int v=edge[i].v;
 46             if (!d[v] && edge[i].flow>0)
 47             {
 48                 d[v]=d[u]+1;
 49                 Q.push(v);
 50                 if (v==to) return 1;
 51             }
 52         }
 53     }
 54     return 0;
 55 }
 56
 57 int dfs(int u,int flow)
 58 {
 59     if (u==to || flow==0) return flow;
 60     int cap=flow;
 61     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 62     {
 63         int v=edge[i].v;
 64         if (d[v]==d[u]+1 && edge[i].flow>0)
 65         {
 66             int x=dfs(v,min(cap,edge[i].flow));
 67             cap -= x;
 68             edge[i].flow -= x;
 69             edge[i^1].flow += x;
 70             if (cap==0) return flow;
 71         }
 72     }
 73     return flow-cap;
 74 }
 75
 76 int dinic()
 77 {
 78     int sum=0;
 79     while (bfs()) sum += dfs(from,inf);
 80     return sum;
 81 }
 82
 83 int main()
 84 {
 85     while (scanf("%d%d%d",&n,&F,&D)!=EOF)
 86     {
 87         memset(head,-1,sizeof(head));
 88         edgenum=0;
 89         from=F+2*n+D+1;
 90         to=from+1;
 91         int a;
 92         for (int i=1 ;i<=F ;i++)
 93         {
 94             scanf("%d",&a);
 95             add(from,i,a);
 96         }
 97         for (int i=1 ;i<=D ;i++)
 98         {
 99             scanf("%d",&a);
100             add(F+2*n+i,to,a);
101         }
102         char str[maxn];
103         memset(str,0,sizeof(str));
104         for (int i=1 ;i<=n ;i++)
105         {
106             scanf("%s",str+1);
107             for (int j=1 ;j<=F ;j++)
108             {
109                 if (str[j]=='Y')
110                     add(j,F+i,1);
111             }
112         }
113         for (int i=1 ;i<=n ;i++)
114         {
115             scanf("%s",str+1);
116             for (int j=1 ;j<=D ;j++)
117             {
118                 if (str[j]=='Y')
119                     add(F+n+i,F+2*n+j,1);
120             }
121         }
122         for (int i=1 ;i<=n ;i++)
123             add(F+i,F+n+i,1);
124         printf("%d\n",dinic());
125     }
126     return 0;
127 }

转载于:https://www.cnblogs.com/huangxf/p/4324636.html

hdu 4292 Food 最大流相关推荐

  1. HDU - 4292 Food(最大流+思维建边)

    题目链接:点击查看 题目大意:作为食堂管理人,现在有n个学生需要打饭,每个学生需要一个饮料和食物才能满足,每个学生可以同时接受多种不同的食物和饮料,现在给出每种食物和饮料的个数,问最多能让多少学生满足 ...

  2. hdu 3572(最大流)

    建图:把每个任务和每一天都看做一个点,添加源点和汇点.源点与每个任务之间连一条边,容量为完成该任务所需处理次数.若第i个任务可以在Si至Ei天处理,则由该任务向这些天分别连一条边,容量为1,表示此任务 ...

  3. hdu 1532(最大流)

    解题思路: 这是一道典型的模板题,直接套用EK算法即可...我感觉最大流的本质就是能否找到一个从源点到汇点的增广路径,并将其最小的边作为增加值,沿着增广路上的边进行更新. AC: #include&l ...

  4. HDU - 3126 Nova(最大流+二分+简单几何)

    题目链接:点击查看 题目大意:给出一个二维平面坐标系,其中有n个巫师,m个敌人,以及k棵树,规定每个巫师都有一个攻击范围,可以攻击以巫师为圆心,以攻击范围为半径,形成的圆内的所有敌人,对于每棵树都有一 ...

  5. hdu 4322 最大费用流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 #include <cstdio> #include <cstring> ...

  6. HDU 4292 Food(dinic +拆点)

    题目链接 我做的伤心了,不知是模版效率低,还是错了,交上就是TLE,找了份别人的代码,改了好几下终于过了.. 1 #include <cstdio> 2 #include <cstr ...

  7. Food HDU - 4292 网络流

    题意: 有n个人,f种食物和d种饮料,每种饮料和食物的个数已知,假设一个人拿到一个自己喜欢的食物或饮料,那个人就会感到满足. 最多能有几个人满足? 思路:拆点建图. 一个人必须又要饮料又要食物,假设我 ...

  8. HDU 4292 Food (成都赛区网络赛第五题,拆点网络流)

      题意:跟POJ3281基本上一样的拆点网络流. 建图:建一超级源点和一超级汇点,源点与食物相连,边权为其数量,汇点与饮料相连,边权也为其数量,把人分成两个点,之间的边权为1.每个人与之需要的食物和 ...

  9. HDU 4411 Arrest 费用流

    题目描述: Description There are (N+1) cities on TAT island. City 0 is where police headquarter located. ...

最新文章

  1. java设计模式:Singleton模式
  2. 包r语言_R语言代码共享:制作R包
  3. 腾讯技术工程沙龙深圳站来了,我们一起聊聊“工业互联网”
  4. 2440 8字数码管 显示0到10 c语言,51单片机对8位数码管依次显示0-7的设计
  5. ubuntu 转mysql_(转)Ubuntu 16.04下安装MySQL
  6. 苹果备忘录怎么调字体大小_苹果手机的备忘录怎么恢复?不知道的快来看看
  7. python二进制移位_python移位运算的实现
  8. esp8266 防掉线方法_ESP8266-12F 中断
  9. leetcode 979. 在二叉树中分配硬币
  10. 单片机、ARM与DSP对比
  11. 汇编语言中sbb是什么意思_汇编语言里 sub是 什么功能?
  12. 【JAVA】逻辑控制
  13. [当人工智能遇上安全] 5.基于机器学习算法的主机恶意代码识别研究
  14. 目标检测系列文章之SPP-net【2015】
  15. 学会JavaScript函数式编程(第3部分)
  16. Ubuntu之ufw安装和使用
  17. 全网最新最全的 HDFS 文件纠删码技术分析
  18. [解读REST] 1.REST的起源
  19. canvas 变形记——移动、旋转、缩放、变形
  20. Solidworks工程图如何使用,替换图纸格式模板文件

热门文章

  1. echart中拆线点的偏移_Real BIM | Rhino+Grasshopper在双曲异形玻璃幕墙中的应用
  2. C语言 | 读写文件
  3. java 工程新建ivy文件_Hadoop学习之路(八)在eclispe上搭建Hadoop开发环境
  4. python正则表达式面试_Python面试 Re-正则表达式
  5. java比较equlse_java基础知识要点
  6. 谷歌发布android+8.2,谷歌Pixel 2代三款新机齐曝光:骁龙835+Android 8.0
  7. pat 乙级 1027 打印沙漏(C++)
  8. 二层和三层工业交换机的主要参数说明
  9. POE工业交换机的四种接法详解
  10. 什么是光纤以太网交换机?