hdu4292 网络流 —最大流

Food

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


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

题意:有N头牛,F种食物,D种饮料;

有2*N组输入,第一组1-N表示第i头牛对F种食物的喜欢与否,Y表示喜欢没N表示不喜欢,第二组1-N表示对饮料的喜欢与否;

问最多有几头牛可以吃上自己喜欢的食物,并喝上自己喜欢的饮料;

最大流算法, S——F(food)——N(n头牛)——N(n头牛)——D(饮料)——T;S表示超级源点,T表示超级汇点;

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stdlib.h>
const int inf=0x3f3f3f3f;
const int MAXN=1e5+10;
const int MAXM=400010;
using namespace std;
struct Node
{
    int to,next,cap;
}edge[MAXM];
int tol;int head[MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;
    edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++;
}
int sap(int start,int end0,int nodenum)
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memcpy(cur,head,sizeof(head));
    int u=pre[start]=start,maxflow=0,aug=-1;
    gap[0]=nodenum;
    while(dis[start]<nodenum)
    {
        loop:
        for(int  &i=cur[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&dis[u]==dis[v]+1)
            {
                if(aug==-1||aug>edge[i].cap)
                    aug=edge[i].cap;
                pre[v]=u;
                u=v;
                if(v==end0)
                {
                    maxflow+=aug;
                    for(u=pre[u];v!=start;v=u,u=pre[u])
                    {
                        edge[cur[u]].cap-=aug;
                        edge[cur[u]^1].cap+=aug;
                    }
                    aug=-1;
                }
                goto loop;
            }
        }
        int mindis=nodenum;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&mindis>dis[v])
            {
                cur[u]=i;
                mindis=dis[v];
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return maxflow;
}
int S,T;
char str[555];
int N,F,D;
int food[MAXN],dirk[MAXN];
int main()
{

    while(scanf("%d%d%d",&N,&F,&D)!=-1)
    {
        S=0;
        T=F+D+N*2+1;
        init();
        for(int i=1; i<=F; i++)
        {
             scanf("%d",&food[i]);
            // cin>>food[i];
             addedge(S,i,food[i]);
        }

        for(int i=1; i<=D; i++)
        {
            scanf("%d",&dirk[i]);
             // cin>>dirk[i];
               addedge(i+F+N*2,T,dirk[i]);
        }
        for(int i=1; i<=N; i++)
            addedge(i+F,i+N+F,1);

        for(int i=1; i<=N; i++)
        {
            scanf("%s",str);
            //cin>>str;
            int l=strlen(str);
            for(int j=0; j<l; j++)
            {
                if(str[j]=='Y')
                    addedge(j+1,F+i,1);
            }

        }
        for(int i=1; i<=N; i++)
        {
            scanf("%s",str);
            //cin>>str;
            int l=strlen(str);
            for(int j=0; j<l; j++)
            {
                if(str[j]=='Y')
                    addedge(F+N+i,F+N*2+j+1,1);
            }

        }
        printf("%d\n",sap(S,T,T+1));
        //cout<<max_flow(0,hui)<<endl;

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值