解题报告 之 HDU4292 Food
Problem 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.
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).
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
题目大意:n个人,f种食物,d种饮料,分别给出每个人对每种食物和饮料是否接受,Y/N。最后问最多能满足多少个人的需求。
分析:典型最大流题。属于多源点和多汇点的题目,还存在拆点,并将两个限制条件放在拆的点左右两边。感觉建图比较容易出错。首先多源点汇点的处理办法是建立一个超级源点和汇点,将多源点转化为单源点。那么超级源点对每个源点的边最大负载应该是多大呢?此题应该就是食物数那么大,因为从某食物源点出发的流不会超过这个值。然后将每个人拆点为两个,边负载为1,再按照Y/N将食物源点与人连起来,负载定为1或INF都行,因为其他地方会限制。重点来了,将饮料放在汇点之前,因为食物不好与饮料直接相连。处理办法同刚才对称。那么最后就求超级源点到超级汇点的最大流即可。
上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 1510;
const int MAXM = 200010;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next;
int cap;
};
Edge edge[MAXM];
int N, F, D;
int cnt, src, des;
//cnt表示一共有几条边
int head[MAXN];
//head[i]表示节点i的第一条边的编号
int level[MAXN];
void addedge(int from, int to, int cap)
{
edge[cnt].to = to;
edge[cnt].cap = cap;
edge[cnt].next = head[from];
head[from] = cnt++;
edge[cnt].to = from;
edge[cnt].cap = 0;
edge[cnt].next = head[to];
head[to] = cnt++;
}
int bfs()
{
queue<int> q;
while (!q.empty())
q.pop();
memset(level, -1, sizeof(level));
level[src] = 0; //源点的深度为0
q.push(src);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > 0 && level[v] == -1) //该边未访问过且该边有流量
{
level[v] = level[u] + 1;
q.push(v);
}
}
}
return level[des] != -1; //返回是否还存在连通路
}
int dfs(int u, int f)
{
if (u == des)
return f;
int tem;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > 0 && level[v] == level[u] + 1)
{
tem = dfs(v, min(f, edge[i].cap));
if (tem > 0)
{
edge[i].cap -= tem;
edge[i ^ 1].cap += tem; //更新边与反向边的流量
return tem;
}
}
}
level[u] = -1; //表示已经走过该点
return 0;
}
int Dinic()
{
int ans = 0, tem;
while (bfs())//如果还存在连通路
{
while (tem = dfs(src, INF)) //一直试图寻找增广路
{
ans += tem;
}
}
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
char str[220];
while (~scanf("%d%d%d", &N, &F, &D))
{
cnt = 0;
memset(head, -1, sizeof(head));
int f, d;
src = 0, des = F + 2 * N + D + 1;
for (int i = 1; i <= F; i++)
{
scanf("%d", &f);
addedge(src, i, f);
}
for (int i = F + 2 * N + 1; i <= F + 2 * N + D; i++)
{
scanf("%d", &d);
addedge(i, des, d);
}
for (int i = 1; i <= N; i++)
{
scanf("%s", str);
for (int j = 0; j < F; j++)
if (str[j] == 'Y')
addedge(j + 1, F + i, 1);
}
for (int i = 1; i <= N; i++)
{
scanf("%s", str);
for (int j = 0; j < D; j++)
if (str[j] == 'Y')
addedge(F + N + i, F + 2 * N + j + 1, 1);
}
for (int i = F + 1; i <= F + N; i++)
addedge(i, i + N, 1);
printf("%d\n", Dinic());
}
return 0;
}