HDU 4292 - Food 网络流dinic

 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种饮料,每个人都有喜欢和不喜欢的食物和饮料,问最多能满足多少人

 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#define inf 0x3f3f3f3f
#define maxn 200100
using namespace std;
int N,F,D,cnt,ss;
int dist[803],head[803];
char s1[220];
struct node
{
    int v,cap,to;
} s[maxn];
queue<int>q;
void init()
{
    memset(s,0,sizeof(s));
    memset(head,-1,sizeof(head));
    cnt=0;
}
void add(int u,int v,int cap)
{
    s[cnt].v=v;
    s[cnt].cap=cap;
    s[cnt].to=head[u];
    head[u]=cnt++;//正向边
    s[cnt].v=u;
    s[cnt].cap=0;
    s[cnt].to=head[v];
    head[v]=cnt++;//反向边
}
int bfs()
{

    memset(dist,0,sizeof(dist));
    dist[0]=1;
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int e=head[u]; ~e; e=s[e].to)
        {
            int v=s[e].v,cap=s[e].cap;
            if(!dist[v]&&cap>0)
            {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
    //printf("%d\n",ss+1);
    return dist[ss+1]!=0;
//    if(dist[ss+1]==0)
//        return false;
//    return true;
}
int dfs(int u,int flow)
{
    int mm;
    if(u==ss+1)
        return flow;
    for(int e=head[u]; ~e; e=s[e].to)
    {
        int v=s[e].v,cap=s[e].cap;
        if(dist[v]==dist[u]+1&&cap>0&&(mm=dfs(v,min(cap,flow))))
        {
            s[e].cap-=mm;
            s[e^1].cap+=mm;
            return mm;
        }
    }
    dist[u]=-1;
    return 0;
}
int dinic()
{
    int ans=0;
    int tf;
    //printf("***\n");
    while(bfs())
    {
       // printf("**\n");
        while(tf=dfs(0,inf))
        {
            ans+=tf;
           // printf("%d\n",tf);
        }

    }
    return ans;
}

int main()
{
    while(~scanf("%d%d%d",&N,&F,&D))
    {
        init();
        ss=2*N+F+D;
        int a;
        for(int i=1; i<=F; i++)
        {
            scanf("%d",&a);
            add(0,i,a);
        }
        for(int i=1; i<=D; i++)
        {
            scanf("%d",&a);
            add(2*N+F+i,ss+1,a);
        }
        for(int i=1; i<=N; i++)
        {
            scanf("%s",s1);
            for(int j=0; j<F; j++)
            {
                if(s1[j]=='Y')
                    add(j+1,F+i,1);
            }
        }
        for(int i=1; i<=N; i++)
        {
            add(F+i,F+N+i,1);
            scanf("%s",s1);
            for(int j=0; j<D; j++)
            {
                if(s1[j]=='Y')
                    add(F+N+i,F+2*N+j+1,1);
            }
        }
        int nn=dinic();
        printf("%d\n",nn);
    }
    return 0;
}

建图: 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值