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

题目大意:
这个就是一个牛吃草问题
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m, s, t;
void init(int n)
{
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
} int main()
{
int n, fo, di;
while(scanf("%d%d%d", &n, &fo, &di)!=EOF)
{
init(*n+fo+di+);
s = , t = * n + fo + di + ;
for (int i = ; i <= fo; i++)
{
int num;
scanf("%d", &num);
add(s, i, num);
}
for (int i = ; i <= di; i++)
{
int num;
scanf("%d", &num);
add(i + fo + * n, t, num);
}
for (int i = ; i <= n; i++)
{
add(i + fo, i + n + fo, );
}
for (int i = ; i <= n; i++)
{
char qw[];
scanf("%s", qw);
for (int j = ; j < fo; j++)
{
if (qw[j] == 'Y') add(j + , i + fo, );
}
}
for (int i = ; i <= n; i++)
{
char qw[];
scanf("%s", qw);
for (int j = ; j < di; j++)
{
if (qw[j] == 'Y') add(i + fo + n, j + fo + * n + , );
}
}
int ans = Maxflow(s, t);
printf("%d\n", ans);
}
return ;
}
												

最新文章

  1. Object.create()方法的低版本兼容问题
  2. 数组遍历map和each使用
  3. 2013/8/28 JS+HTML 三级省市区联动
  4. 关于css命名规范
  5. python之6-1常用函数
  6. python高级编程:缓存
  7. HibernateDaoSupport的getSession()与HibernateTemplate的区别
  8. python_如何为创建大量实例节省内存?
  9. web.config文件中配置数据库连接的两种方式
  10. Linux常用监控命令简介 - top
  11. 混合编译.c/.cpp与.cu文件
  12. unity3d-游戏实战突出重围,整合游戏
  13. 在UnrealEngine4中使用Google Protobuf
  14. MIT-6.828-JOS-lab2:Memory management
  15. GlusterFS实战
  16. 八、面向对象模型(用例图,序列图,类图,生成Java源代码及Java源代码生成类图)
  17. 利用层序遍历(含空节点)和中序遍历重建二叉树 python
  18. BAT大数据面试题
  19. 将XML格式的字符串封装成DOM对象
  20. pandas(八)重塑和轴向旋转

热门文章

  1. Golang中的Gosched、Goexit、GOMAXPROCS
  2. DVWA渗透笔记
  3. 多线程设置flag标志位实现同步
  4. layoutInflater参数解析与源码分析
  5. 这本最适合夯实基础的经典 Java 书籍,可能80% 的 Java 程序员没有认真看过!
  6. python教程:使用 async 和 await 协程进行并发编程
  7. python从零开始基础入门——开发环境搭建:Visual Studio Code
  8. G - Pairs Forming LCM LightOJ - 1236 (质因子分解)
  9. Git敏捷开发--stash命令
  10. Numpy学习-(1)