Food

点击打开链接

Food

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


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


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 0x1f1f1f1f
int s,u;
int c[1000][1000],r[1000][1000];
int gap[1000];
int dis[1000];
void init()
{
    int v,x,q[1000],front=0,rear=0;
    memset(gap,0,sizeof(gap));
    memset(dis,0xff,sizeof(dis));
    q[rear++]=u;
    dis[u]=0;
    while(front<rear)
    {
        x=q[front++];
        gap[dis[x]]++;
        for(v=1;v<u;v++)
        {
            if(dis[v]==-1&&c[v][x]>0)
            {
                dis[v]=dis[x]+1;
                q[rear++]=v;
            }
        }
    }
}
int max_flow()
{
    init();
    int flow=0,top=s,i,j,k,pre[1200],low[1000];
    memset(low,0,sizeof(low));
    while(dis[s]<u)
    {
        int flag=0;
        low[s]=INF;
        for(i=1;i<=u;i++)
            if(c[top][i]>0&&dis[top]==dis[i]+1&&dis[i]>=0)
            {
                flag=1;
                break;
            }
        if(flag)
        {
            low[i]=c[top][i];
            if(low[i]>low[top])
                low[i]=low[top];
            pre[i]=top;
            top=i;
            if(top==u)
            {
                flow+=low[u];
                j=top;
                while(j!=s)
                {
                    k=pre[j];
                    c[k][j]-=low[u];
                    c[j][k]+=low[u];
                    j=k;
                }
                top=s;
                memset(low,0,sizeof(low));
            }
        }
        else
        {
            int min=u-1;
            for(j=1;j<u;j++)
            {
                if(c[top][j]>0&&dis[j]+1<min&&dis[j]>=0)
                    min=dis[j]+1;
            }
            gap[dis[top]]--;
            if(gap[dis[top]]==0)
                break;
            gap[min]++;
            dis[top]=min;
            if(top!=s)
                top=pre[top];
        }
    }
    return flow;
}
int food[555];
int drink[555];
char ok[222][222];
char tt[222];
int main()
{
    int i,D,F,N,f,d,k,j;
    while(scanf("%d%d%d",&N,&F,&D)!=EOF)
    {
        memset(c,0,sizeof(c));
        s=1;
        u=2+N*2+F+D;
        for(i=1;i<=F;i++)
            scanf("%d",&food[i]);
        for(i=1;i<=D;i++)
            scanf("%d",&drink[i]);
        for(i=2;i<=1+F;i++)
            c[s][i]=food[i-1];
        for(i=F+2;i<=F+1+N;i++)
            c[i][i+N]=1; // 人人之间
        for(i=F+2*N+2;i<=F+2*N+1+D;i++)
            c[i][u]=drink[i-F-2*N-1];
        for(i=1;i<=N;i++)
        {
            scanf("%s",tt+1);
            for(j=1;j<=F;j++)
                if(tt[j]=='Y')
                    c[1+j][F+1+i]=1;
        }
        for(i=1;i<=N;i++)
        {
            scanf("%s",tt+1);
            for(j=1;j<=D;j++)
                if(tt[j]=='Y')
                    c[F+1+N+i][F+1+2*N+j]=1;
        }
        printf("%d\n",max_flow());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值