Dining POJ - 3281(最大流)

Dining POJ - 3281

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).InputLine 1: Three space-separated integers: N, F, and D

Lines 2…N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.OutputLine 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
Sample Input
4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

Hin
One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.Sponsor

题意:
有一些牛,一堆食物,一堆饮料。一头牛要吃一份食物喝一份饮料才算满足,而且牛对某些食物和饮料才有好感,问最多有多少头牛是满足的

思路:两次匹配
注意:《挑程》上的板子比较慢,会T,下面的板子,可以A。

#include <cstdio>
#include <vector>
#include <algorithm>

#define SZ(v) (int)v.size()

const int INF = 1e9;
const int MAXN = 1010;
const int MAXM = 1e5 + 10;

namespace MaxFlow {
    struct Edge {
        int v, rev, f;
    };

    int n, s, t;
    int cur[MAXM], dep[MAXN], gap[MAXN];
    int flow;
    std::vector<Edge> G[MAXN];

    void add_edge(int u, int v, int f) {
        G[u].push_back({v, SZ(G[v]), f});
        G[v].push_back({u, SZ(G[u]) - 1, 0});
    }

    int dfs(int u, int lim) {
        if (u == t) return lim;
        int num = 0, f;
        for (int &i = cur[u], v; i < SZ(G[u]); ++i) {
            if (dep[v = G[u][i].v] == dep[u] - 1 && (f = G[u][i].f))
                if (G[u][i].f -= (f = dfs(v, std::min(lim - num, f))),
                        G[v][G[u][i].rev].f += f, (num += f) == lim)
                    return num;
        }
        if (!--gap[dep[u]++]) dep[s] = n + 1;
        return ++gap[dep[u]], cur[u] = 0, num;
    }

    void init(int _n) {
        n = _n;
        for (int i = 0; i < n; ++i) G[i].clear();
    }

    void solve(int _s, int _t) {
        s = _s, t = _t, flow = 0;
        for (int i = 0; i <= n; ++i) cur[i] = dep[i] = gap[i] = 0;
        for (gap[0] = n; dep[s] <= n; flow += dfs(s, INF));
    }
}

using MaxFlow::add_edge;

int main() {
    int N, F, D;
    scanf("%d%d%d", &N, &F, &D);
    int s = F + D + 2 * N + 1, t = s + 1;
    MaxFlow::init(t + 1);
    for(int i = 1; i <= N; i++) {
        int pos1 = i + F + D;
        int pos2 = i + F + D + N;
        int f[102], d[102], ff, dd;
        scanf("%d%d", &ff, &dd);
        for(int j = 1; j <= ff; j++) {
            scanf("%d", &f[j]);
            add_edge(f[j], pos1, 1);
        }
        for(int j = 1; j <= dd; j++) {
            scanf("%d", &d[j]);
            add_edge(pos2, d[j] + F, 1);
        }
    }
    //两头牛
    for(int i = 1; i <= N; i++) {
        add_edge(F + D + i, F + D + N + i, 1);
    }
    //s和食物
    for(int i = 1; i <= F; i++) {
        add_edge(s, i, 1);
    }
    //t和饮料
    for(int i = 1; i <= D; i++) {
        add_edge(F + i, t, 1);
    }
    MaxFlow::solve(s, t);
    printf("%d\n", MaxFlow::flow);
}

挑程,会T

#include <iostream>
#include <vector>
#include <queue>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAX_V= 1e3 + 10;
const int INF = 0x3f3f3f3f;
bool used[MAX_V];
struct edge{
    int to, cap, rev;
    edge(int to, int cap, int rev):to(to),cap(cap),rev(rev){}
};
vector<edge> G[MAX_V];

void add_edge(int from, int to, int cap) {
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
}

int dfs(int s, int t, int f) {
    if(s == t) return f;
    used[s] = true;
    for (int i = 0; i < G[s].size(); i++) {
        edge &e = G[s][i];
        if (!used[e.to] && e.cap > 0) {
            int d = dfs(e.to, t, min(f, e.cap));
            if(d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
}

int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
        memset(used, 0, sizeof(used));
        int f = dfs(s, t, INF);
        if(f == 0) return f;
        flow += f;
    }
}

int main() {
    int N, F, D;
    scanf("%d%d%d", &N, &F, &D);
    int s = 0, t = F + D + 2 * N + 1;
    for(int i = 1; i <= N; i++) {
        int f[102], d[102], ff, dd;
        scanf("%d%d", &ff, &dd);
        for(int j = 1; j <= ff; j++) {
            scanf("%d", &f[j]);
            add_edge(f[j], i + F, 1);
        }
        for(int j = 1; j <= dd; j++) {
            scanf("%d", &d[j]);
            add_edge(i + F + N, F + 2 * N + d[j], 1);
        }
    }
    //两头牛
    for(int i = 1; i <= N; i++) {
        add_edge(F + i, F + N + i, 1);
    }
    //s和食物
    for(int i = 1; i <= F; i++) {
        add_edge(s, i, 1);
    }
    //t和饮料
    for(int i = 1; i <= D; i++) {
        add_edge(F + 2 * N + i, t, 1);
    }
    printf("%d\n", max_flow(s, t));
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值