Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4452 Accepted Submission(s): 1507
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
题意:已知人数,food种类以及数量,drink种类以及数量,令给出每个顾客的要求,顾客得到food和drink即为满足,问最多满足多少顾客。最大流问题。
CODE:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define INF 0x3f3f3f3f
using namespace std;
#define MAXN 100005
#define MAXM 2005
struct node
{
int v,next,flow;
} edge[MAXN];
int index,head[MAXN];
void add_edge(int u, int v, int flow)
{
edge[index].v=v;
edge[index].flow=flow;
edge[index].next=head[u];
head[u]=index++;
edge[index].v=u;
edge[index].flow=0;
edge[index].next=head[v];
head[v]=index++;
}
int deep[MAXN];
bool search_(int s,int t)
{
memset(deep,0,sizeof(deep));
queue<int> q;
q.push(s);
deep[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
if(u==t)return 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!deep[v]&&edge[i].flow)
{
deep[v]=deep[u]+1;
q.push(v);
}
}
}
return 0;
}
int DFS(int s,int t,int min_)
{
if(s==t)return min_;
int i,ans=0,f;
for(i=head[s]; i!=-1 &&ans<min_; i=edge[i].next)
{
int v=edge[i].v;
if(deep[v]==deep[s]+1 && edge[i].flow)
{
f=DFS(v,t,min(min_-ans,edge[i].flow));
edge[i].flow-=f;
edge[i^1].flow+=f;
ans+=f;
if(ans==min_)return ans;
}
}
return ans;
}
int dinic(int s,int t)
{
int ans=0,p;
while(search_(s,t))
{
while(p=DFS(s,t,INF))
ans+=p;
}
return ans;
}
int food[MAXM],drink[MAXM];
char str[MAXM];
int main()
{
int n,f,d;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
int start=0,end=2*n+f+d+1;
index=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=f; ++i)
{
scanf("%d",&food[i]);
add_edge(start,i,food[i]);
}
for(int i=1; i<=d; ++i)
{
scanf("%d",&drink[i]);
add_edge(f+n*2+i,end,drink[i]);
}
for(int i=1; i<=n; ++i)
{
scanf("%s",str);
for(int j=0; j<f; ++j)
if(str[j]=='Y')
add_edge(j+1,i+f,1);
}
for(int i=1; i<=n; ++i)
add_edge(f+i,f+i+n,1);
for(int i=1; i<=n; ++i)
{
scanf("%s",str);
for(int j=0; j<d; ++j)
if(str[j]=='Y')
add_edge(i+f+n,n*2+f+j+1,1);
}
int ans=dinic(start,end);
printf("%d\n",ans);
}
return 0;
}