hdu 4292 Food (最大流)

hdu 4292 Food

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

题目大意:给出客人数量,食物种类,饮品种类,每种食物和饮品的库存,还有每位顾客的要求。求出,最多能满足多少位客人的要求。

解题思路:跟poj 3281 Dining是一种类型的题目。 设置超级源点,连向所有的食物,容量为该食物的库存,设置超级汇点,使所有饮品指向超级汇点,容量为该饮品的库存。然后客户连接对应的食物和饮品,容量为1。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 20005;
const int M = 200005;
const int OF = 300;
const int INF = 0x3f3f3f3f;
const int FIN = 1999;
typedef long long ll;
int n, f, d, s, t;
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];

void addEdge(int a,int b,int c) {
    to[ec] = b;
    v[ec] = c;
    Next[ec] = first[a];
    first[a] = ec++;

    to[ec] = a;
    v[ec] = 0;
    Next[ec] = first[b];
    first[b] = ec++;
}

int BFS() {
    int kid, now, f = 0, r = 1, i;
    memset(lev, 0, sizeof(lev));
    que[0] = s, lev[s] = 1;
    while (f < r) {
        now = que[f++];
        for (i = first[now]; i != -1; i = Next[i]) {
            kid = to[i];
            if (!lev[kid] && v[i]) {
                lev[kid] = lev[now] + 1;
                if (kid == t) return 1;
                que[r++] = kid;
            }
        }
    }
    return 0;
}

int DFS(int now, int sum) {
    int kid, flow, rt = 0;
    if (now == t) return sum;
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
        head[now] = i;
        kid = to[i];
        if (lev[kid] == lev[now] + 1 && v[i]) {
            flow = DFS(kid, min(sum - rt, v[i]));
            if (flow) {
                v[i] -= flow;
                v[i^1] += flow;
                rt += flow;
            } else lev[kid] = -1;
        }
    }
    return rt;
}

int dinic() {
    int ans = 0;
    while (BFS()) {
        for (int i = 0; i <= t; i++) {
            head[i] = first[i];
        }
        ans += DFS(s, INF);
    }
    return ans;
}   

char c[205];
void input() {
    int a;
    for (int i = 1; i <= f; i++) {
        scanf("%d", &a);
        addEdge(s, i, a);
    }
    for (int i = 1; i <= d; i++) {
        scanf("%d", &a);
        addEdge(i + 3 * OF, t, a);
    }
    for (int i = 1; i <= n; i++) {
        addEdge(i + OF, i + 2 * OF, 1);
    }
    getchar();
    for (int i = 1; i <= n; i++) {
        scanf("%s", c);
        for (int j = 0; j < f; j++) {
            if (c[j] == ‘Y‘) {
                addEdge(j + 1, i + OF, 1);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        scanf("%s", c);
        for (int j = 0; j < d; j++) {
            if (c[j] == ‘Y‘) {
                addEdge(i + 2 * OF, (j + 1) + 3 * OF, 1);
            }
        }
    }
}

int main() {
    while (scanf("%d %d %d", &n, &f, &d) == 3) {
        ec = 0;
        memset(first, -1, sizeof(first));
        s = 0, t = FIN;
        input();
        printf("%d\n", dinic());
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。

时间: 2025-01-05 20:32:57

hdu 4292 Food (最大流)的相关文章

hdu 4292 网络最大流

http://acm.hdu.edu.cn/showproblem.php?pid=4292 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 colle

hdu 4292 Food 最大流+拆点

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2664    Accepted Submission(s): 899 Problem Description You, a part-time dining service worker in your college's dining hall, are now confus

hdu 4292 Food 最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292 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

HDU 4292 Food(建图+最大流)

使用 PVRTC 压缩格式创建纹理(Creating textures in the PVRTC compression format) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 有关该篇

K - Leapin&#39; Lizards - HDU 2732(最大流)

题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有多少蜥蜴不能离开迷宫(跳出迷宫就可以离开了.) 输入描述:输入矩阵的行M和跳跃值D,接着输入两个矩阵(列数自己求),第一个矩阵是每个柱子的耐久值,下面一个矩阵有'L'的表示这个柱子上有一只蜥蜴. 分析:题目明白还是比较容易的,矩阵的点数是20*20,也就400个点,对每个有耐久值的柱子进行拆点,然后

HDU 4862 Jump 费用流

又是一个看了题解以后还坑了一天的题…… 结果最后发现是抄代码的时候少写了一个负号. 题意: 有一个n*m的网格,其中每个格子上都有0~9的数字.现在你可以玩K次游戏. 一次游戏是这样定义的: 你可以选任意之前没有走过的格子作为起点.然后走任意步,其中每一步你可以向右或者向下走任意格.假如从(x1, y1)走到(x2, y2)需要花费能量|x1-x2|+|y1-y2|-1,如果这一步和上一步格子的数字相同,那么可以获得格子上相应数字的能量.能量可以为负值. 问你,在K次以内走完所以格子最多能得到多

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,

hdu 4289 利用最大流思想求图的最小割

http://acm.hdu.edu.cn/showproblem.php?pid=4289 Problem Description You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to anothe

hdu 2853 Assignment 费用流

就是本来就给出了一个匹配,然后让你求一个权值最大的匹配,并且和初始匹配变动最小. #include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int N=400; const int MAXE=20000000; const int inf=1<<30; int head[N],s,t,cnt,ans; int d[N],pre[N]; bool