Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 475 Accepted Submission(s): 222
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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 5010
#define E 800080
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
struct Edge
{
int en,cap,flow,next;
} edge[E];
int head[N] , tot , now[N];
int source,sink,tot_num;
int pre[N] , dis[N] , gap[N];
queue<int> q;
void add_edge(int st,int en,int cap)
{
edge[tot].en=en;
edge[tot].cap=cap;
edge[tot].flow=0;
edge[tot].next=head[st];
head[st]=tot++;
edge[tot].en=st;
edge[tot].cap=0;
edge[tot].flow=0;
edge[tot].next=head[en];
head[en]=tot++;
}
void bfs()
{
memset(dis,-1,sizeof(dis));
memset(gap,0,sizeof(gap));
while(!q.empty()) q.pop();
gap[dis[sink]=0]=1; q.push(sink);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].en;
if(edge[i].cap!=0 || dis[v]!=-1) continue;
q.push(v);
++gap[dis[v]=dis[u]+1];
}
}
}
int sap()
{
memset(pre,-1,sizeof(pre));
bfs();
memcpy(now,head,sizeof(head));
int point=source , flow=0 , min_flow=INT_INF;
while(dis[source]<tot_num)
{
bool fg=false;
for(int i=now[point]; i!=-1; i=edge[i].next)
if(edge[i].cap-edge[i].flow>0 && dis[point]==dis[edge[i].en]+1)
{
min_flow=min(min_flow,edge[i].cap-edge[i].flow);
now[point]=i;
pre[edge[i].en]=point;
point=edge[i].en;
if(point==sink)
{
flow+=min_flow;
for(int u=source; u!=sink; u=edge[now[u]].en)
{
edge[now[u]].flow+=min_flow;
edge[now[u]^1].flow-=min_flow;
}
point=source;
min_flow=INT_INF;
}
fg=true;
break;
}
if(fg) continue;
if(--gap[dis[point]]==0) break;
int Min=tot_num;
for(int i=head[point]; i!=-1; i=edge[i].next)
if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
{
Min=dis[edge[i].en];
now[point]=i;
}
gap[dis[point]=Min+1]++;
if(point!=source) point=pre[point];
}
return flow;
}
char s[400];
void build(int n,int f,int d)
{
memset(head,-1,sizeof(head));
tot=0;
tot_num=f+d+n+n+2; source=0; sink=f+d+n+n+1;
for(int i=1; i<=n; i++)
add_edge(f+i,f+n+d+i,1);
for(int i=1,val; i<=f; i++) //讀入food
{
scanf("%d",&val);
add_edge(source,i,val);
}
for(int i=1,val; i<=d; i++) //讀入drink
{
scanf("%d",&val);
add_edge(f+n+i,sink,val);
}
for(int i=1; i<=n; i++)
{
scanf("%s",s);
for(int j=0; j<(int)strlen(s); j++)
{
if(s[j]=='N') continue;
add_edge(j+1,f+i,1);
}
}
for(int i=1; i<=n; i++)
{
scanf("%s",s);
for(int j=0; j<(int)strlen(s); j++)
{
if(s[j]=='N') continue;
add_edge(f+n+d+i,f+n+j+1,1);
}
}
}
int main()
{
int n,f,d;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
build(n,f,d);
int ans=sap();
printf("%d\n",ans);
}
return 0;
}