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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-24 17:28:36

POJ 3281【拆点 && 最大流经典建图】的相关文章

HDU 3376--Matrix Again【最大费用最大流 &amp;&amp; 经典建图】

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 3457    Accepted Submission(s): 1020 Problem Description Starvae very like play a number game in the n*n Matrix. A positive intege

HPU 2686--Matrix【最大费用最大流 &amp;&amp; 经典建图】

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2062    Accepted Submission(s): 1074 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer numbe

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

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

poj3281-Dining ,最大流,建图

点击打开链接 分析: 求最大流 建图: 拆点 牛拆成左边与食物相连的左牛 和 右边与饮料相连的右牛 1.s->食物 连边 2.食物->左牛 3.左牛->右牛 4.右牛->饮料 5.饮料->t 边的方向为 s->食物->左牛->右牛->饮料->t #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #i

POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10159   Accepted: 4676 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 fabulo

poj 3281 Dining(最大流)

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 prefer

POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17598   Accepted: 7977 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t