HDU 4292 Food (网络流,最大流)

Description

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.

Input

There are several test cases.

For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.

The second line contains F integers, the ith number of which denotes amount of representative food.

The third line contains D integers, the ith number of which denotes amount of representative drink.

Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.

Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.

Please process until EOF (End Of File).

Output

For each test case, please print a single line with one integer, the maximum number of people to be satisfied.

Sample Input

4 3 3

1 1 1

1 1 1

YYN

NYY

YNY

YNY

YNY

YYN

YYN

NNY

Sample Output

3

Http

HDU:https://vjudge.net/problem/HDU-4292

Hit

网络流,最大流

题目大意

现在提供若干种饮料、食物,每种都有一定的数量。给定N个人对每一种食物和饮料的喜好,若给某人提供了其喜欢的食物和饮料,则称这个人是开心的。现在求能使最多的人开心的数量。

解决思路

我们按照源点-食物-人-饮料-汇点的顺序建图。对于每一个食物,从源点连容量为其数量的的边,而对于每一个人,连接他和他所有喜欢的食物,容量都是1,。因为给一个人只提供一份食物和一份饮料,所以我们把人拆点,中间连容量为1的边。而对于饮料则是类似的,从人到他喜欢的饮料连容量为1的边,从饮料到汇点连容量为其数量的边。这样跑最大流即可。

关于最大流,这里使用Dinic,可以参考这篇文章

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; const int maxN=1001;
const int maxM=maxN*maxN*2;
const int inf=2147483647; class Edge
{
public:
int u,v,flow;
}; int N,F,D;//点的编号安排:0汇点,[1,N]人1,[N+1,N*2]人2,[N*2+1,N*2+F]食物,[N*2+F+1,N*2+F+D]饮料,N*2+F+D+1汇点
int cnt=-1;
char str[maxN];
int Head[maxN];
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxN]; void Add_Edge(int u,int v,int flow);
bool bfs();
int dfs(int u,int flow); int main()
{
while (cin>>N>>F>>D)//多组数据
{
cnt=-1;//先清空
memset(Head,-1,sizeof(Head));
for (int i=1;i<=F;i++)//读入食物的数量
{
int maxflow;
scanf("%d",&maxflow);
Add_Edge(0,N*2+i,maxflow);//连接源点和食物
}
for (int i=1;i<=D;i++)//读入饮料的数量
{
int maxflow;
scanf("%d",&maxflow);
Add_Edge(N*2+F+i,N*2+F+D+1,maxflow);//连接饮料和汇点
}
for (int i=1;i<=N;i++)
{
scanf("%s",str);
for (int j=0;j<F;j++)
if (str[j]=='Y')
Add_Edge(N*2+j+1,i,1);//连接人与食物
}
for (int i=1;i<=N;i++)
{
scanf("%s",str);
for (int j=0;j<D;j++)
if (str[j]=='Y')
Add_Edge(N+i,N*2+F+j+1,1);//连接人与饮料
}
for (int i=1;i<=N;i++)
Add_Edge(i,N+i,1);//连接人1与人2,即拆开的两个点
int Ans=0;//求解最大流
while (bfs())
{
for (int i=0;i<=2*N+F+D+1;i++)
cur[i]=Head[i];
while (int di=dfs(0,inf))
Ans+=di;
}
cout<<Ans<<endl;
}
return 0;
} void Add_Edge(int u,int v,int flow)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow; cnt++;
Next[cnt]=Head[v];
Head[v]=cnt;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
} bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
depth[0]=1;
Q[1]=0;
do
{
t++;
int u=Q[t];
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (t!=h);
if (depth[N*2+F+D+1]==-1)
return 0;
return 1;
} int dfs(int u,int flow)
{
if (u==N*2+F+D+1)
return flow;
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}

HDU 4292 Food (网络流,最大流)的更多相关文章

  1. Leapin' Lizards [HDU - 2732]【网络流最大流】

    题目链接 网络流直接最大流就是了,只是要拆点小心一个点的流超出了原本的正常范围才是. #include <iostream> #include <cstdio> #includ ...

  2. H - Food - hdu 4292(简单最大流)

    题目大意:有N个人,然后有F种食品和D种饮料,每个人都有喜欢的饮料和食品,求出来这些食品最多能满足多少人的需求. 输入描述: 分析:以前是做过类似的题目的,不过输入的信息量比较大,还是使用邻接表的好些 ...

  3. hdu 4292 Food 网络流

    题目链接 给你f种食物, 以及每种食物的个数, d种饮料, 以及个数, n个人, 以及每个人可以接受的食物种类和饮料种类. 每个人必须得到一种食物和一种饮料. 问最后得到满足的人的个数. 因为一个人只 ...

  4. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  5. Redraw Beautiful Drawings(hdu4888)网络流+最大流

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...

  6. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  7. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  8. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  9. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

随机推荐

  1. LeetCode Pow(x, n) (快速幂)

    题意 Implement pow(x, n). 求X的N次方. 解法 用正常的办法来做是会超时的,因为可能有21亿次方的情况,所以需要优化一下.这里用到了快速幂算法,简单来说就是将指数分解成二进制的形 ...

  2. Nextcloud私有云盘在Centos7下的部署笔记

    搭建个人云存储一般会想到ownCloud,堪称是自建云存储服务的经典.而Nextcloud是ownCloud原开发团队打造的号称是“下一代”存储.初一看觉得“口气”不小,刚推出来就重新“定义”了Clo ...

  3. Python-元组-10

    元祖 Why:对于容器型数据类型list,无论谁都可以对其增删改查,那么有一些重要的数据放在list中是不安全的,所以需要一种容器类的数据类型存放重要的数据,创建之初只能查看而不能增删改,这种数据类型 ...

  4. [BUAA软工]第一次结对作业

    [BUAA软工]结对作业 本次作业所属课程: 2019BUAA软件工程 本次作业要求: 结对项目 我在本课程的目标: 熟悉结对合作,为团队合作打下基础 本次作业的帮助:理解一个c++ 项目的开发历程 ...

  5. Linux内核分析期中总结

    目录: “Linux内核分析”实验一报告 “Linux内核分析”实验二报告 “Linux内核分析”实验三报告 Linux实验四报告 “Linux内核分析”第五周报告 "Linux内核分析&q ...

  6. Zookeeper 3.4.8分布式安装

    1.机器信息 五台centos 64位机器 2.集群规划 Server Name Hadoop Cluster Zookeeper   Ensemble HBase Cluster Hadoop01 ...

  7. 第三个Sprint冲刺第四天(燃尽图)

  8. vue为app做h5页面,如何做到同域名对应不同版本的h5代码

    1.当我们在做混合开发的时候,app端可以有无数多个版本,一般情况h5页面只有一套代码.应该如何部署多套代码呢? 2.业务场景 当出现这种情况的时候,其实前端可以部署多套代码.比如: www.stat ...

  9. laravel orm 中的一对多关系 hasMany

    个人对于laravel orm 中对于一对多关系的理解 文章表 article,文章自然可以评论,表 comment 记录文章的评论,文章和评论的关系就是一对多,一篇文章可以有多个评论. 在 comm ...

  10. Laravel 常见错误 1071 Specified key was too long

    Laravel 5.5 + Mysql 5.5 ,执行 migrate 时,提示索引长度超过指定的 1000 bytes 原因: Mysql 对索引有一定的长度限制,版本不同长度不同: MyIsAm ...