Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1773 Accepted Submission(s): 638
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
Source
Recommend
liuyiding
让我独自做的话,真看不出这是一个可以转化成求最大流的题。。。这个题你会建图的话,真心好做。那么如何建图呢?我也是参照别人的:
将源点和食物相连容量为食物的数量。然后将饮料与汇点相连,容量为饮料的数量。将人分成两部分,一部分连食物,一部分连饮料,同一个人之间相连,容量为1。然后将食物与人相连,饮料与人相连。最后直接求最大流就行。
为什么要把一个人分成两个人,而不是一个人直接连食物和饮料?求解?
//SAP算法
#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define MIN(a,b) a>b?b:a;
#define M 40007
struct E
{
int v,w,next;
E() {}
E(int v,int w,int next):v(v),w(w),next(next) {}
} edg[900000];
int dis[M],gap[M],list[M],nodes;
int sourse,sink,nn,node;
char s[207];
void addedge(int u,int v,int w)
{
edg[nodes]=E(v,w,list[u]);
list[u]=nodes++;
edg[nodes]=E(u,0,list[v]);
list[v]=nodes++;
}
int dfs(int src,int aug)
{
if(src==sink)return aug;
int left=aug,mindis=nn;
for(int j=list[src]; j!=-1; j=edg[j].next)
{
int v=edg[j].v;
if(edg[j].w)
{
if(dis[v]+1==dis[src])
{
int minn=MIN(left,edg[j].w);
minn=dfs(v,minn);
edg[j].w-=minn;
edg[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)return aug-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==aug)
{
if(!(--gap[dis[src]]))dis[sourse]=nn;
dis[src]=mindis+1;
gap[dis[src]]++;
}
return aug-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,inf);
return ans;
}
int main()
{
int f,d,n,a,b;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
memset(list,-1,sizeof(list));
int e=f+d+n+n+1;
int m,p,sum=0,maxx=0;
for(int i=1; i<=f; i++)
{
scanf("%d",&a);
addedge(0,i,a);
}
for(int i=1; i<=d; i++)
{
scanf("%d",&b);
addedge(i+f+n+n,e,b);
}
for(int i=1; i<=n; i++)
{
scanf("%s",s);
for(int j=0; j<f; j++)
if(s[j]=='Y')
addedge(j+1,i+f,1);
}
for(int i=1; i<=n; i++)
{
scanf("%s",s);
for(int j=0; j<d; j++)
if(s[j]=='Y')
addedge(f+n+i,f+n+n+j+1,1);
}
for(int i=1; i<=n; i++)
addedge(i+f,i+f+n,1);
nodes=0;
sourse=0;
int anss=sap(0,e);
printf("%d\n",anss);
}
return 0;
}