Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1054 Accepted Submission(s): 390
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.
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
Source
代码:
#include<cstdio>
#include<cstring>
const int N=1005;
const int M=200005;
const int INF=0x7fffffff;
int n,s,t,tol;
int head[N],arc[N],pre[N],dis[N],gap[N];
struct node
{
int y,f,nxt;
}edge[M];
void add(int x,int y,int f)
{
edge[tol].y=y;
edge[tol].f=f;
edge[tol].nxt=head[x];
head[x]=tol++;
edge[tol].y=x;
edge[tol].f=0;
edge[tol].nxt=head[y];
head[y]=tol++;
}
void sap()
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memcpy(arc,head,sizeof(dis));
gap[0]=n;
int ans=0,arg=INF,u=pre[s]=s;
while(dis[s]<n)
{
L:
for(int &i=arc[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].y;
if(edge[i].f&&dis[u]==dis[v]+1)
{
if(arg>edge[i].f)arg=edge[i].f;
pre[v]=u;
u=v;
if(v==t)
{
ans+=arg;
for(u=pre[u];v!=s;v=u,u=pre[u])
{
edge[arc[u]].f-=arg;
edge[arc[u]^1].f+=arg;
}
arg=INF;
}
goto L;
}
}
int min=n;
for(int i=head[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].y;
if(edge[i].f&&min>dis[v])
{
arc[u]=i;
min=dis[v];
}
}
if(--gap[dis[u]]==0)break;
dis[u]=min+1;
gap[dis[u]]++;
u=pre[u];
}
printf("%d\n",ans);
}
int main()
{
int i,j,N,F,D;
while(~scanf("%d%d%d",&N,&F,&D))
{
tol=0;
memset(head,-1,sizeof(head));
s=0;t=2*N+F+D+1;
int f,d;
n=F+D+2*N+2;
for(i=1;i<=F;i++)
{
scanf("%d",&f);
add(s,i,f);
}
for(i=F+2*N+1;i<=n-2;i++)
{
scanf("%d",&d);
add(i,t,d);
}
for(i=F+1;i<=F+N;i++)add(i,i+N,1);
char op[205];
for(i=1;i<=N;i++)
{
scanf("%s",op+1);
for(j=1;j<=F;j++)
{
if(op[j]=='Y')add(j,F+i,1);
}
}
for(i=1;i<=N;i++)
{
scanf("%s",op+1);
for(j=1;j<=D;j++)
{
if(op[j]=='Y')add(F+N+i,F+2*N+j,1);
}
}
sap();
}
return 0;
}