Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4150 Accepted Submission(s): 1382
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种饮料,已经给出每种食物、饮料的数目。输入下面有N*F和N*D两个矩阵,N*F矩阵的第i行第j列表示第i个人是否喜欢第j种食物,若该位置的元素为Y表示喜欢否则不喜欢。
N*D矩阵的第i行第j列表示第i个人是否喜欢第j种饮料,若该位置的元素为Y表示喜欢否则不喜欢。
每个人的需求是——选择一份食物和一份饮料(必须是他们喜欢的),问最多可以满足几个人的需求。
解析:
比较常规的最大流问题,见图比较好想,要注意的是要拆点见图,对人拆点,把每个人拆成两个点,为左点和右点,两点之间容量为1,这样才能确保每个人只选择一种食物和饮料。具体见图如下:
(1)源点向食物建边,容量为食物的数目。
(2)饮料向汇点建边,容量为饮料的数目。
(3)左点和喜欢的食物建边,容量为1 。
(4)右点和喜欢的饮料建边,容量为1。
(5)左点和右点之间建边,容量为1,确保每个人只选择一种食物和饮料。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1100
#define maxm 1100000
#define INF 0x3f3f3f3f
using namespace std;
int N, F, D;
int head[maxn], cur[maxn], cnt;
int vis[maxn], dist[maxn];
int outset, inset;
char map[1000];
struct node {
int u, v, cap, flow,next;
};
node edge[maxm];
void init(){
cnt = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int w){
edge[cnt] = {u, v, w, 0, head[u]};
head[u] = cnt++;
edge[cnt] = {v, u, 0, 0, head[v]};
head[v] = cnt++;
}
void getmap(){
int a, b;
outset = 0;
inset = 2 * N + D + F + 1;
for(int i = 1; i <= F; ++i){//源点到食物建边
scanf("%d", &a);
add(outset, 2 * N + i, a);
}
for(int i = 1; i <= D; ++i){
scanf("%d", &a);
add(2 * N + F + i, inset, a);//饮料到汇点建边
}
//人到喜欢的食物建边
for(int i = 1; i <= N; ++i){
scanf("%s", map);
for(int j = 0; j < F; ++j)
if(map[j] == 'Y')
add(2 * N + j + 1, i, 1);
}
//人到喜欢的饮料建边
for(int i = 1; i <= N; ++i){
scanf("%s", map);
for(int j = 0; j < D; ++j)
if(map[j] == 'Y')
add(i + N, 2 * N + F + j + 1, 1);
}
//对人拆点,这样才能确保每个人只选择一种食物和饮料
for(int i = 1; i <= N; ++i)
add(i, i + N, 1);
}
bool BFS(int st, int ed){
queue<int>q;
memset(vis, 0, sizeof(vis));
memset(dist, -1, sizeof(dist));
vis[st] = 1;
dist[st] = 0;
q.push(st);
while(!q.empty()){
int u =q.front();
q.pop();
for(int i = head[u]; i != -1; i = edge[i].next){
node E = edge[i];
if(!vis[E.v] && E.cap > E.flow){
vis[E.v] = 1;
dist[E.v] = dist[u] + 1;
if(E.v == ed) return true;
q.push(E.v);
}
}
}
return false;
}
int DFS(int x, int ed, int a){
if(x == ed || a == 0)
return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next){
node &E = edge[i];
if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
E.flow += f;
edge[i ^ 1].flow -= f;
a -= f;
flow += f;
if(a == 0) break;
}
}
return flow;
}
int maxflow(int st, int ed){
int flowsum = 0;
while(BFS(st, ed)){
memcpy(cur, head, sizeof(head));
flowsum += DFS(st, ed, INF);
}
return flowsum;
}
int main(){
while(scanf("%d%d%d", &N, &F, &D) != EOF){
init();
getmap();
printf("%d\n", maxflow(outset, inset));
}
return 0;
}