POJ 3281【拆点 && 最大流经典建图】

本文深入探讨了信息技术领域的关键组件和技术应用,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发等细分领域。通过具体实例阐述了如何在不同场景下灵活运用这些技术和工具,旨在帮助开发者提升技术水平,拓宽技术视野。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11097 Accepted: 5096

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: NF, 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 iwill 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

Hint

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.

题目大意:有F种食物,D种饮料,N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份),一种食物被一头牛吃了之后,其余牛就不能吃了。
第一行有N,F,D三个整数
接着2-N+1行代表第i头牛,前面两个整数是Fi与Di(食物与饮料的种类数量),接着是食物的种类与饮料的种类
要求输出最多分配能够满足的牛的数量

错误思路:建立超级源点和超级汇点。让源点指向食物,食物指向牛,牛再指向饮料,最后让饮料指向汇点,中间所有边权为1。求最大流。

 食物指向牛,牛再指向饮料。因为这样不能保证一头牛可能得到多种食物和多种饮料

 

正确思路:建立超级源点和超级汇点,把每头牛拆分成左,右牛。让源点 --> 食物 --> 左牛 -- > 右牛 --> 饮料 --> 汇点,所有边权为1。求最大流。


源点:0

食物:2 n + 1 -- 2 n + f

左牛:1 -- n

右牛:n + 1 -- 2 n

饮料:2n + f + 1 -- 2n + f  + d

汇点:2n + f + d + 1


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define maxn 1000
#define maxm 200000 + 2000
#define INF 0x3f3f3f3f
using namespace std;

int dist[maxn], vis[maxn];
int cur[maxn], head[maxn], cnt;
int N, F, D;
int sect;//超级汇点
struct node {
    int u, v, cap, flow, next;
};

node edge[maxm];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, 0, head[u]}; //正建边
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]}; //反建边
    head[v] = cnt++;
}

void getmap(){
    int Fans, Dans;
    for(int i = 1; i <= N; ++i){
        scanf("%d%d", &Fans, &Dans);
        while(Fans--){
            int num;
            scanf("%d", &num);
            add(2 * N + num, i, 1);//食物和左牛连接
        }
        while(Dans--){
            int num;
            scanf("%d", &num);
            add(N + i, 2 * N + F + num, 1);//右牛和饮料连接
        }
        add(i, i + N, 1);//左牛和右牛连接
    }
    sect = 2 * N + F + D + 1;
    for(int i = 1; i <= F; ++i)
        add(0, 2 * N + i, 1);//源点和食物连接
    for(int i = 1; i <= D; ++i)
        add(2 * N + F + i, 2 * N + F + D + 1, 1);//饮料和超级汇点连接
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(dist, -1, sizeof(dist));
    memset(vis, 0 ,sizeof(vis));
    q.push(st);
    dist[st] = 0;
    vis[st] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow  = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int sumflow = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        sumflow += DFS(st, ed, INF);
    }
    return sumflow;
}

int main (){
    while(scanf("%d%d%d", &N, &F, &D) != EOF){
        init();
        getmap();
        printf("%d\n", maxflow(0, sect));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值