题目:
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.
输入:
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, please print a single line with one integer, the maximum number of people to be satisfied.
样例输入:
4 3 3 1 1 1 1 1 1 YYN NYY YNY YNY YNY YYN YYN NNY
样例输出:
3
这个题和Dining那个题差不多,都是拆点。
建立一个超级源点和一个超级汇点,把源点和F中食物连接,汇点和D种饮料连接,将人1和人2连接,根据输入,将每个人喜欢的饮料和食物各自连接,然后跑一遍最大流即可。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100000;
const int INF=0x7fffffff;
char str[210];
int head[maxn],cur[maxn],dis[maxn];
int cnt;
int s,t;
struct edge
{
int u,v,c,next;
}node[maxn*4];
void add(int u,int v,int c)
{
node[cnt].u=u;
node[cnt].v=v;
node[cnt].c=c;
node[cnt].next=head[u];
head[u]=cnt++;
}
bool bfs()
{
queue<int>q;
memset(dis,0,sizeof(dis));
dis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=node[i].next)
{
edge e=node[i];
if(!dis[e.v]&&e.c>0)
{
dis[e.v]=dis[e.u]+1;
q.push(e.v);
if(e.v==t)return 1;
}
}
}
return dis[t]!=0;
}
int dfs(int u,int mx)
{
int ret=0;
if(u==t||mx==0)return mx;
for(int &i=cur[u];i!=-1;i=node[i].next)
{
edge e=node[i];
if(dis[e.v]==dis[e.u]+1&&e.c>0)
{
int w=dfs(e.v,min(mx,e.c));
node[i].c-=w;
node[i^1].c+=w;
ret+=w;
mx-=w;
if(mx==0)break;
}
}
return ret;
}
int dinic()
{
int maxflow=0;
while(bfs())
{
memcpy(cur,head,sizeof(head));
maxflow+=dfs(s,INF);
}
return maxflow;
}
int main()
{
int N,F,D;
while(~scanf("%d%d%d",&N,&F,&D))
{
memset(head,-1,sizeof(head));
cnt=0;
s=0;t=F+D+N+N+100;
for(int i=1;i<=F;i++)
{
int num;
scanf("%d",&num);
add(s,i,num);
add(i,s,0);
}
for(int i=1;i<=D;i++)
{
int num;
scanf("%d",&num);
add(F+i,t,num);
add(t,F+i,0);
}
for(int i=1;i<=N;i++)
{
scanf("%s",str);
for(int j=0;j<F;j++)
{
if(str[j]=='Y')
{
add(j+1,F+D+i,1);
add(F+D+i,j+1,0);
}
}
}
for(int i=1;i<=N;i++)
{
scanf("%s",str);
for(int j=0;j<D;j++)
{
if(str[j]=='Y')
{
add(F+D+N+i,F+j+1,1);
add(F+j+1,F+D+N+i,0);
}
}
}
for(int i=1;i<=N;i++)
{
add(F+D+i,F+D+N+i,1);
add(F+D+N+i,F+D+i,0);
}
printf("%d\n",dinic());
}
}