hdu4292Food【拆点网络流】

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
 

将近一个小时的成果==早上5点多睡不着突然想到的做法居然真的有bug 唉

题意:为顾客服务食物和饮料,每个人喜欢某些食物和某些饮料,上了她不喜欢的东西他就走了,不同食物饮料有特定的供应量,问最多多少人满意==最初的想法当然是左边加一个源点,右边加一个汇点,根据已知条件连边,然而需要注意的是:刚开始不理解为啥拆点,想一个极端的例子,只有一个人,所有食物和饮料他都喜欢,但是最大流一定只是1!!所以要通过拆点限制人

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=1000000;
const int mn=22222;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],nxt[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c1,int c2)
{
    reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;
    reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=nxt[i])
            if(flow[i]&&dis[v=reach[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=nxt[i])
        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }dis[u]--;
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}

int main()
{
   // freopen("cin.txt","r",stdin);
    int n,d,f;
    while(~scanf("%d%d%d",&n,&f,&d))
    {
        prepare(n*2+f+d+2,0,f+n*2+d+1);
        for(int i=1;i<=f;i++)
        {
            int a;
            scanf("%d",&a);
            addedge(src,i,a,0);
        }
        for(int i=f+n*2+1;i<=f+n*2+d;i++)
        {
            int a;
            scanf("%d",&a);
            addedge(i,dest,a,0);
        }
        for(int i=f+1;i<=f+n;i++)
            addedge(i,i+n,1,0);
        char str[300];
        for(int i=f+1;i<=f+n;i++)
        {
            scanf("%s",str);
            int len=strlen(str);
            for(int j=0;j<len;j++)
                if(str[j]=='Y')
                    addedge(j+1,i,1,0);
        }
        for(int i=f+1+n;i<=f+n*2;i++)
        {
            scanf("%s",str);
            int len=strlen(str);
            for(int j=0;j<len;j++)
                if(str[j]=='Y')
                    addedge(i,j+1+f+n*2,1,0);
        }
        printf("%d\n",Dinic_flow());
    }
    return 0;
}
六点半开始学习的感觉真爽~~~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值