poj 3281 Dining (网络流)

Description

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).

Input

Line 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.

Output

Line 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

3

题目翻译

现在有N头牛及F种食物和D种饮料,每种食物或饮料只能供一只牛享用,且每头牛只能享用一种食物及一种饮料。问最多使几头牛享用它们喜爱的食物。

建图很简单,1~F为食物,F+1~F+N为牛的数量,同时将一头牛拆成两个点,再建F+N+1~F+2N为“牛的分身”,每头牛和它的分身之间连一条权值为1的边,来保证每头牛只能吃到一种食物和喝到一种水。然后F+2N+1~F+2N+D为水。0为源点,每个食物与它连边(权值可以为1);F+2N+D+1为汇点,每种水与它连边(权值可以为1);牛本体和它可以吃的东西连边,牛分身和它可以喝的东西连边。跑最大流即可。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3fffffff
using namespace std;

struct Edge {
    int v, next, f;
}edge[20005];
int N, F, D, s, t, num = 1, head[20005], vis[20005], queue[20005], dis[20005];

void add(int u, int v, int f) {
    num ++;
    edge[num].v = v;
    edge[num].f = f;
    edge[num].next = head[u];
    head[u] = num;
    num ++;
    edge[num].v = u;
    edge[num].f = 0;
    edge[num].next = head[v];
    head[v] = num;
}

bool bfs() {
    int h = 0, tail = 1;
    queue[1] = s;
    dis[s] = 0;
    vis[s] = true;
    do {
        int u = queue[++ h];
        for(int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].v;
            if(! vis[v] && edge[i].f) {
                vis[v] = true;
                dis[v] = dis[u] + 1;
                queue[++ tail] = v;
            }
        }
    } while(h < tail);
    if(vis[t]) return true;
    return false; 
}

int dfs(int u, int delta) {
    if(u == t || delta == 0) return delta;
    int ans = 0;
    for(int i = head[u]; i && delta; i = edge[i].next) {
        int v = edge[i].v;
        if(dis[v] == dis[u] + 1 && edge[i].f) {
            int dd = dfs(v, min(delta, edge[i].f));
            edge[i].f -= dd;
            edge[i ^ 1].f += dd;
            delta -= dd;
            ans += dd;
            if(! delta) break;
        }
    }
    if(! ans) dis[u] = -1;
    return ans;
}

int Maxflow() {
    int ans = 0;
    while(1) {
        memset(dis, 0, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        memset(queue, 0, sizeof(queue));
        if(! bfs()) break;
        ans += dfs(s, INF);
    }
    return ans;
}

int main() {
    scanf("%d%d%d", &N, &F, &D);
    s = 0; t = F + 2 * N + D + 1;
    for(int i = 1; i <= F; i ++)
        add(s, i, 1);
    for(int i = F + 2 * N + 1; i <= F + 2 * N + D; i ++)
        add(i, t, 1);
    for(int i = F + 1; i <= F + N; i ++)
        add(i, i + N, 1);
    for(int i = 1; i <= N; i ++) {
        int fi, di;
        scanf("%d%d", &fi, &di);
        for(int j = 1; j <= fi; j ++) {
            int u;
            scanf("%d", &u);
            add(u, F + i, 1);
        }
        for(int j = 1; j <= di; j ++) {
            int v;
            scanf("%d", &v);
            add(i + F + N, v + F + 2 * N, 1);
        }
    }
    printf("%d", Maxflow());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值