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
题目大意:给出客人数量,食物种类,饮品种类,每种食物和饮品的库存,还有每位顾客的要求。求出,最多能满足多少位客人的要求。
和这道题 https://blog.csdn.net/henucm/article/details/81783486 一样不过这道题的食物 人 饮料 是用图表示的
然后我使用邻接表写的 挺方便的 建图 见上面那篇 有分析
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#define N 0x3f3f3f3f
#define ll long long
using namespace std;
struct ac
{
int w;
int nxt;
int v;
}r[201000];
int per[2000],vis[2000],cur[2000];
int cnt;
int s,e;
int t,n,m;
void add(int u,int v,int w)
{
r[cnt].v=v;
r[cnt].w=w;
r[cnt].nxt=per[u];
per[u]=cnt++;
r[cnt].v=u;
r[cnt].w=0;
r[cnt].nxt=per[v];
per[v]=cnt++;
}
int bfs()
{
memset(vis,-1,sizeof(vis));
queue<int>q;
vis[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=per[u];i+1;i=r[i].nxt)
{
if(vis[r[i].v]==-1&&r[i].w>0)
{
vis[r[i].v]=vis[u]+1;
q.push(r[i].v);
}
}
}
if(vis[e]>-1)
return 1;
return 0;
}
int dfs(int u,int h)
{
if(u==e||h==0)
return h;
int ans,he=0;
for(int &i=cur[u];i+1;i=r[i].nxt)
{
if(vis[r[i].v]==vis[u]+1&&r[i].w&&(ans=dfs(r[i].v,min(h,r[i].w))))
{
r[i].w-=ans;
r[i^1].w+=ans;
h-=ans;
he+=ans;
if(h==0)
break;
}
}
return he;
}
int main()
{
while(cin>>t>>n>>m)
{
s=0,e=2*t+n+m+1;
cnt=0;
memset(per,-1,sizeof(per));
for(int i=1;i<=t;i++)
{
add(2*i-1,2*i,1);
}
for(int i=1;i<=n;i++)//吃的
{
int x;
cin>>x;
add(s,2*t+i,x);
}
for(int i=1;i<=m;i++)//喝的
{
int x;
cin>>x;
add(t*2+n+i,e,x);
}
for(int i=1;i<=t;i++)//吃的
{
getchar();
for(int j=1;j<=n;j++)
{
char c;
cin>>c;
if(c=='Y')
add(2*t+j,2*i-1,1);
}
}
for(int i=1;i<=t;i++)//吃的
{
getchar();
for(int j=1;j<=m;j++)
{
char c;
cin>>c;
if(c=='Y')
add(2*i,2*t+n+j,1);
}
}
int sum=0;
while(bfs())
{
for(int i=0;i<=e;i++)
cur[i]=per[i];
sum+=dfs(s,N);
}
cout<<sum<<endl;
}
return 0;
}