HDU 4292 Food (最大流)

Food

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


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

 

ISAP算法:

 

/*
  首先把食物,人和饮料都看成点。构图方法是拆点,将每个人拆成两个点。
  然后构造一个源点和一个汇点,连接方式为源点-食物-人-人-饮料-汇点。
  其中源点跟食物的边的容量为食物的数量,然后把人与他要吃的食物连边,
  容量为1。人再与拆出来的自己连一条边,容量也为1。拆出来的人
  与他要喝的饮料也连边,容量为1,最后饮料再与汇点连边,容量为饮料的
  数量。构图完成,直接做最大流。
  
  至于拆点的理由是,要保证每个人只选了一种食物和一种饮料,要避免多选
  的情况。
  
  187MS 2176K 
  */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 1024
#define inf 0x7fffffff

using namespace std;

struct node
{
    int to,val,next;
}edge[SIZE*SIZE];

int N,F,D;
int head[SIZE],idx;
int gap[SIZE],dis[SIZE];
int source,sink,pt;
char str[256];

void addnode(int from,int to,int val)
{
    edge[idx].to = to;
    edge[idx].val = val;
    edge[idx].next = head[from];
    head[from] = idx ++;
    edge[idx].to = from;
    edge[idx].val = 0;
    edge[idx].next = head[to];
    head[to] = idx ++;
}

int dfs(int cur,int cur_val)
{
    if(cur == sink) return cur_val;
    int min_dis = pt - 1, temp_val = cur_val;
    for(int i=head[cur]; i!=-1; i=edge[i].next)
    {
        int to = edge[i].to, to_val = edge[i].val;
        if(to_val > 0)
        {
            if(dis[to] + 1 == dis[cur])
            {
                int val = dfs(to,min(to_val,temp_val));
                temp_val -= val;
                edge[i].val -= val;
                edge[i^1].val += val;
                if(dis[source] >= pt)
                    return cur_val-temp_val;
                if(temp_val == 0)
                    break;
            }
            if(dis[to] < min_dis)
                min_dis = dis[to];
        }
    }
    if(cur_val == temp_val)
    {
        --gap[dis[cur]];
        if(!gap[dis[cur]])
            dis[source] = pt;
        dis[cur] = min_dis + 1;
        ++gap[dis[cur]];
    }
    return cur_val-temp_val;
}

void sap()
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    int ret = 0;
    gap[0] = pt;
    while(dis[source] < pt)
        ret += dfs(source,inf);
    printf("%d\n",ret);
}

void read()
{
    source = 0;
    sink = 2*N + F + D + 1;
    pt = sink + 1;
    idx = 0;
    memset(head,-1,sizeof(head));
    int t;
    for(int i=1; i<=F; i++)
    {
        scanf("%d",&t);
        addnode(source,i,t);
    }
    for(int i=1; i<=D; i++)
    {
        scanf("%d",&t);
        addnode(F+2*N+i,sink,t);
    }
    for(int i=1; i<=N; i++)
        addnode(F+i,F+N+i,1);
    for(int i=1; i<=N; i++)
    {
        scanf("%s",str);
        for(int j=0; j<(int)strlen(str); j++)
            if(str[j] == 'Y')addnode(j+1,F+i,1);
    }
    for(int i=1; i<=N; i++)
    {
        scanf("%s",str);
        for(int j=0; j<(int)strlen(str); j++)
            if(str[j] == 'Y')addnode(F+N+i,F+2*N+j+1,1);
    }
}

int main()
{
    while(~scanf("%d%d%d",&N,&F,&D))
    {
        read();
        sap();
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值