Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7592    Accepted Submission(s): 2533


 

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

2012 ACM/ICPC Asia Regional Chengdu Online

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  4288 4296 4295 4294 4293 

题意:每个人都有喜欢的和不喜欢的食物和饮品,看提供的东西,最多能满足多少人。

这个题跟那个dining 很相似啊。

/*
 首先把食物,人和饮料都看成点。构图方法是拆点,将每个人拆成两个点。
  然后构造一个源点和一个汇点,连接方式为源点-食物-人-人-饮料-汇点。
  其中源点跟食物的边的容量为食物的数量,然后把人与他要吃的食物连边,
  容量为1。人再与拆出来的自己连一条边,容量也为1。拆出来的人
  与他要喝的饮料也连边,容量为1,最后饮料再与汇点连边,容量为饮料的
  数量。构图完成,直接做最大流。

  至于拆点的理由是,要保证每个人只选了一种食物和一种饮料,要避免多选
  的情况
   0(源点)--->f--->P---->P'---->d--->D(汇点)
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=400000;
struct Edge
{
    int v,cap,next;
}edge[maxn];
int first[maxn],cnt,n,f,d,D,dep[maxn];
void add_edge(int u,int v,int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=first[u];
    first[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=first[v];
    first[v]=cnt++;
}
int bfs()
{
    queue<int >q;
    while(q.size()) q.pop();
    memset(dep,0,sizeof(dep));
    dep[0]=1;//0是源点
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=first[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dep[v]==0&&edge[i].cap>0)
            {
                dep[v]=dep[u]+1;
                q.push(v);
            }
        }
    }
    if(dep[D+1]==0) return 0;
    return 1;
}
int dfs(int u,int flow)
{
    int cost=0;
    int temp;
    if(u==D+1)return flow;
    for(int i=first[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(dep[v]==dep[u]+1&&edge[i].cap>0)
        {
            temp=dfs(v,min(flow-cost,edge[i].cap));
            edge[i].cap-=temp;
            edge[i^1].cap+=temp;
            cost+=temp;
            if(cost==flow)
                return  cost;
        }
    }
    if(cost==0) dep[u]=-1;//表示不能到达节点
    return cost;
}
int Dinic()
{
    int ans=0,temp;
    while(bfs())
    {
        //cout<<ans<<endl;
        while(temp=dfs(0,inf))
        {
            ans+=temp;
        }
    }
    return ans;
}
int main()
{
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        D=2*n+f+d;
        cnt=0;
        memset(first,-1,sizeof(first));
        for(int i=1;i<=f;i++)//食物
        {//源点要食物建一条有向边,最大流量为吃的数量
            int x;
            scanf("%d",&x);
            add_edge(0,i,x);
        }
        for(int i=1;i<=d;i++)
        {//喝的到汇点建一条有向边,最大流量为喝的数量
            int x;
            scanf("%d",&x);
            add_edge(n*2+f+i,D+1,x);
        }
        for(int i=1;i<=n;i++)
        {//食物到人建一条有向边,最大流量为1
            getchar();
            for(int j=1;j<=f;j++)
            {
                char ch;
                scanf("%c",&ch);
                if(ch=='Y')
                    add_edge(j,f+i,1);
            }
        }
        for(int i=1;i<=n;i++)
        {//人到喝的建一条有向边,最大流量为1
            add_edge(f+i,n+f+i,1);
            getchar();
            for(int j=1;j<=d;j++)
            {
                char ch;
                scanf("%c",&ch);
                if(ch=='Y')
                    add_edge(n+f+i,2*n+f+j,1);
            }
        }cout<<Dinic()<<endl;
    }return 0;
}