1. 程式人生 > >hdu 4292 Food (最大流)

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;
}

相關推薦

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 pro

HDU-4292-Food,拆點,Dinic+向前星注意!!此題我認為超坑啊!!!

  Problem Description   You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem

Food

Food http://acm.hdu.edu.cn/showproblem.php?pid=4292 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other

Hdu 4280 Island Transport

search acc describes main end ble acm erl rect Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536

[luoguP2766] 長遞增子序列問題

close spl 方法 emp 路徑 pid code display div 傳送門 題解來自網絡流24題: 【問題分析】 第一問時LIS,動態規劃求解,第二問和第三問用網絡最大流解決。 【建模方法】 首先動態規劃求出F[i],表示以第i位為開頭的最長上

[cogs729]圓桌問題

for isp front 集合 targe 一個人 getchar 分析 freopen 傳送門 模型 二分圖多重匹配問題,可以用最大流解決。 實現 建立二分圖,每個單位為X集合中的頂點,每個餐桌為Y集合中的頂點,增設附加源S和匯T。 1、從S向每個Xi頂

poj 1273 Drainage Ditches

for from cstring ref farm info pat fun instead poj 1273 Drainage Ditches 對增廣路,最大流不知太熟悉,看這裏 Description Every time it rains o

【網絡24題】星際轉移問題網絡判定

log ima 網絡 http alt 最大流 星際 網絡流24題 ges 然後判斷什麽時候流量到達k就可以了。【網絡流24題】星際轉移問題(最大流)(網絡判定)

【網絡24題】騎士共存問題

itl 求一個 src 最大 tle font put 計算 國際 【codevs1922】騎士共存問題 題目描述 Description 在一個n*n個方格的國際象棋棋盤上,馬(騎士)可以攻擊的棋盤方格如圖所示。棋盤上某些方格設置了障礙,騎士不

【網絡24題】魔術球問題

names -m 枚舉 ble 因此 efi ron += sin 【網絡流24題】魔術球問題(最大流) 題面 Cogs 題解 是不是像極了最小路徑覆蓋? 因此,我們枚舉放到哪一個球(也可以二分) 然後類似於最小路徑覆蓋的連邊 因為一根柱子對應一個路徑的覆蓋 所以,提前預處

【網絡24題】方格取數問題

ace nic 最小 stdin getch esp mar memset pty 【網絡流24題】方格取數問題(最大流) 題面 Cogs 題解 首先,相鄰的只能出現一個,每個點要麽選,要麽不選。 所以不難想到最小割 所以,將棋盤黑白染色後 將某種顏色的格子從源點連過去,容

【網絡24題】圓桌聚餐

cpp pan pos har pre cst ++ get post 【網絡流24題】圓桌聚餐(最大流) 題面 Cogs 題解 這道題很簡單 首先每個單位的人數限制 直接從源點向單位連邊,容量為人數 同樣的, 每個桌子向匯點連邊,容量為可以坐的人數 因為每個桌子只能夠做一

UVa 753 - A Plug for UNIX

printf ron 所有 ont flow new 使用 string can 鏈接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob

UVa 11082 - Matrix Decompressing

增加 new cst 根據 ros 輸入 memset times mincut 鏈接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob

Annual Congress of MUD

sep edge after empty real-time test span called per Annual Congress of MUD 時間限制: 1 Sec 內存限制: 128 MB提交: 80 解決: 10[提交] [狀態] [討論版] [命題人:a

Dining

Dining http://poj.org/problem?id=3281 Time Limit: 2000MS   Memory Limit: 65536K Total Submissio

POJ3436:ACM Computer Factory

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 3738

HDU4280:Island Transport

Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 13187 

2015年ACM/ICPC北京賽區 D題

參考網上題解: 題意:給定一顆類似於dnf、lol點天賦時候的天賦(技能)有向樹,技能獲得的前提是他的前置技能都獲得了,作為一個RMB玩家,你有特權: 1.直接花費一定數量的錢獲得某個技能。 2.花費一定數量的錢將一個技能的某一個前置關係取消,即將前置技能到該技

HDU2732:Leapin' Lizards

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4250 &nbs