Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1668 Accepted Submission(s): 606
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
每个满足的人只要提供一份食物,和一份饮料即可,
将人拆点,超级源点,连食物,边权为食物数量,超级汇点连饮料,边权为数量,将食物与人一点相连,另一点与饮料相连
求最大流
#include <cstring>
#include <cstdio>
#include <queue>
#define MAXN 50000
#define MAXM 500000
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int u,v,f;
};
node e[MAXM*3];
int first[MAXN],next[MAXM*3];
int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];
int cc;
inline void add_edge(int u,int v,int f)
{
e[cc].u=u;
e[cc].v=v;
e[cc].f=f;
next[cc]=first[u];
first[u]=cc;
cc++;
e[cc].u=v;
e[cc].v=u;
e[cc].f=0;
next[cc]=first[v];
first[v]=cc;
cc++;
}
int ISAP(int s,int t,int n)
{
int cur_flow,flow_ans=0,u,tmp,neck,i,v;
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=n;i++)
curedge[i]=first[i];
gap[0]=n+1;
u=s;
while(d[s]<=n)
{
if(u==t)
{
cur_flow=inf;
for(i=s;i!=t;i=e[curedge[i]].v)
{
if(cur_flow>e[curedge[i]].f)
{
neck=i;
cur_flow=e[curedge[i]].f;
}
}
for(i=s;i!=t;i=e[curedge[i]].v)
{
tmp=curedge[i];
e[tmp].f-=cur_flow;
e[tmp^1].f+=cur_flow;
}
flow_ans+=cur_flow;
u=neck;
}
for(i=curedge[u];i!=-1;i=next[i])
{
v=e[i].v;
if(e[i].f&&d[u]==d[v]+1)
break;
}
if(i!=-1)
{
curedge[u]=i;
pre[v]=u;
u=v;
}
else
{
if(0==--gap[d[u]])
break;
curedge[u]=first[u];
for(tmp=n+5,i=first[u];i!=-1;i=next[i])
if(e[i].f)
tmp=min(tmp,d[e[i].v]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=s)
u=pre[u];
}
}
return flow_ans;
}
int main()
{
int N,F,D;
while(scanf("%d%d%d",&N,&F,&D)!=EOF)
{
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
int s=0,t=F+D+N+N+1;
int i;
for(i=1;i<=F;i++)
{
int a;
scanf("%d",&a);
add_edge(s,i,a);
}
for(i=1;i<=D;i++)
{
int a;
scanf("%d",&a);
add_edge(i+F,t,a);
}
for(i=1;i<=N;i++)
{
add_edge(F+D+i,F+D+i+N,1);
}
char str[250];
for(i=1;i<=N;i++)
{
scanf("%s",str+1);
int j;
for(j=1;j<=F;j++)
{
if(str[j]=='Y')
{
add_edge(j,F+D+i,1);
}
}
}
for(i=1;i<=N;i++)
{
scanf("%s",str+1);
int j;
for(j=1;j<=D;j++)
{
if(str[j]=='Y')
{
add_edge(F+D+i+N,F+j,1);
}
}
}
int res=ISAP(s,t,t);
printf("%d\n",res);
}
return 0;
}