HDU 4292 Food 最大流(贵在建图)

本文介绍了一种通过最大流算法解决实际问题的方法——即如何最大化餐厅服务人员满足顾客饮食偏好的人数。文章详细解释了如何构建图模型,并使用SAP算法求解最大流,最终实现目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

 

Food

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


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
 


 

Recommend
liuyiding
 
 
让我独自做的话,真看不出这是一个可以转化成求最大流的题。。。这个题你会建图的话,真心好做。那么如何建图呢?我也是参照别人的:
将源点和食物相连容量为食物的数量。然后将饮料与汇点相连,容量为饮料的数量。将人分成两部分,一部分连食物,一部分连饮料,同一个人之间相连,容量为1。然后将食物与人相连,饮料与人相连。最后直接求最大流就行。
为什么要把一个人分成两个人,而不是一个人直接连食物和饮料?求解?
//SAP算法
#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define MIN(a,b) a>b?b:a;
#define M 40007
struct E
{
    int v,w,next;
    E() {}
    E(int v,int w,int next):v(v),w(w),next(next) {}
} edg[900000];
int dis[M],gap[M],list[M],nodes;
int sourse,sink,nn,node;
char s[207];
void addedge(int u,int v,int w)
{
    edg[nodes]=E(v,w,list[u]);
    list[u]=nodes++;
    edg[nodes]=E(u,0,list[v]);
    list[v]=nodes++;
}
int dfs(int src,int aug)
{
    if(src==sink)return aug;
    int left=aug,mindis=nn;
    for(int j=list[src]; j!=-1; j=edg[j].next)
    {
        int v=edg[j].v;
        if(edg[j].w)
        {
            if(dis[v]+1==dis[src])
            {
                int minn=MIN(left,edg[j].w);
                minn=dfs(v,minn);
                edg[j].w-=minn;
                edg[j^1].w+=minn;
                left-=minn;
                if(dis[sourse]>=nn)return aug-left;
                if(left==0)break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }

    if(left==aug)
    {
        if(!(--gap[dis[src]]))dis[sourse]=nn;
        dis[src]=mindis+1;
        gap[dis[src]]++;
    }
    return aug-left;
}
int sap(int s,int e)
{
    int ans=0;
    nn=e+1;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    sourse=s;
    sink=e;
    while(dis[sourse]<nn)
        ans+=dfs(sourse,inf);
    return ans;
}
int main()
{
    int f,d,n,a,b;
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        memset(list,-1,sizeof(list));
        int e=f+d+n+n+1;
        int m,p,sum=0,maxx=0;
        for(int i=1; i<=f; i++)
        {
            scanf("%d",&a);
            addedge(0,i,a);
        }
        for(int i=1; i<=d; i++)
        {
            scanf("%d",&b);
            addedge(i+f+n+n,e,b);
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s);
            for(int j=0; j<f; j++)
                if(s[j]=='Y')
                    addedge(j+1,i+f,1);
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s);
            for(int j=0; j<d; j++)
                if(s[j]=='Y')
                    addedge(f+n+i,f+n+n+j+1,1);
        }
        for(int i=1; i<=n; i++)
            addedge(i+f,i+f+n,1);
        nodes=0;
        sourse=0;
        int anss=sap(0,e);
        printf("%d\n",anss);
    }

    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值