HDU 4292 FOOD (最大流)

题意:n个人,f种食物,d种饮料,给出食物和饮料数量,以及每个人的喜好,求让人满意的最大人数。

题解:最大流
人数要最多,那么每个人只能吃一种食物和饮料。
跟这题一样,拆点,只不过多了食物和饮料的数量。

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
/*
 * SAP 邻接矩阵形式
 * 点的编号从 0 开始
 * 增加个 flow 数组,保留原矩阵 maze, 可用于多次使用最大流
 */
const int MAXN = 1010;
int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN];//存最大流的流量
int sap(int start, int end, int nodenum) {
    memset(cur, 0, sizeof(cur));
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    memset(flow, 0, sizeof(flow));
    int u = pre[start] = start, maxflow = 0, aug = -1;
    gap[0] = nodenum;
    while (dis[start] < nodenum) {
    loop:
        for (int v = cur[u]; v < nodenum; v++)
            if (maze[u][v] - flow[u][v] && dis[u] == dis[v] + 1) {
                if (aug == -1 || aug > maze[u][v] - flow[u][v]) aug = maze[u][v] - flow[u][v];
                pre[v] = u;
                u = cur[u] = v;
                if (v == end) {
                    maxflow += aug;
                    for (u = pre[u]; v != start; v = u, u = pre[u]) {
                        flow[u][v] += aug;
                        flow[v][u] -= aug;
                    }
                    aug = -1;
                }
                goto loop;
            }
        int mindis = nodenum - 1;
        for (int v = 0; v < nodenum; v++)
            if (maze[u][v] - flow[u][v] && mindis > dis[v]) {
                cur[u] = v;
                mindis = dis[v];
            }
        if ((--gap[dis[u]]) == 0) break;
        gap[dis[u] = mindis + 1]++;
        u = pre[u];
    }
    return maxflow;
}
int n, f, d, x;
char s[MAXN];
int main() {
    while (~scanf("%d%d%d", &n, &f, &d)) {
        memset(maze, 0, sizeof(maze));
        for (int i = 1; i <= f; i++) {
            scanf("%d", &x);
            maze[0][i] = x;
        }
        for (int i = 1; i <= d; i++) {
            scanf("%d", &x);
            maze[i + f + 2 * n][f + d + 2 * n + 1] = x;
        }
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            for (int j = 1; j <= f; j++) {
                if (s[j] == 'N') continue;
                maze[j][f + i] = 1;
            }
        }
        for (int i = 1; i <= n; i++) maze[f + i][f + n + i] = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            for (int j = 1; j <= d; j++) {
                if (s[j] == 'N') continue;
                maze[f + n + i][f + 2 * n + j] = 1;
            }
        }
        printf("%d\n", sap(0, f + 2 * n + d + 1, f + 2 * n + d + 2));
    }
	return 0;
}

你可能感兴趣的:(#,网络流)