poj 3281(构图+网络流)

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14144   Accepted: 6425

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

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.

Source

题意:有N头牛,F种食物,D种饮料,第I头牛喜欢Fi种食物,Di种饮料已知一头牛最多能吃一种食物和一种饮料,每种饮料或食物最多能被一头牛吃,求以上条件下,最多能有多少头牛能吃到他所喜爱的食物和饮料。

构图:S向每种食物连容量1的边,每种食物向喜爱它的牛连容量1的边,将牛拆点,I向I’连容量1的边,I’向它喜爱的饮料连容量1的边,每种饮料向T连容量1的边。最大流。

总结一句话:限制某一个元素的使用次数:拆点之后在其中连一条容量为限制次数的边.

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 5005;
const int INF = 999999999;
struct Edge{
    int v,next;
    int w;
}edge[N*N];
int head[N];
int level[N];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k){
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des){
    queue<int >q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0){
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des) return increaseRoad;
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v;
        int w = edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            edge[k].w -=w;
            edge[k^1].w+=w;
            ret+=w;
            if(ret==increaseRoad) return ret;
        }
    }
    return ret;
}
LL Dinic(int src,int des){
    LL ans = 0;
    while(BFS(src,des)!=-1) ans+=(LL)dfs(src,des,INF*1.0);
    return ans;
}

int main(){
    int n,f,d;
    while(scanf("%d%d%d",&n,&f,&d)!=EOF){
        init();
        int src = 0,des = 2*n+f+d+1;
        for(int i=1;i<=f;i++) addEdge(src,i,1,tot);
        for(int i=1;i<=d;i++) addEdge(2*n+f+i,des,1,tot);
        for(int i=f+1;i<=f+n;i++){
            addEdge(i,i+n,1,tot);
            int a,b,c;
            scanf("%d%d",&a,&b);
            for(int j=1;j<=a;j++){
                scanf("%d",&c);
                addEdge(c,i,1,tot);
            }
            for(int j=1;j<=b;j++){
                scanf("%d",&c);
                addEdge(i+n,2*n+f+c,1,tot);
            }

        }
        printf("%d\n",Dinic(src,des));
    }
}
时间: 2024-10-01 07:02:41

poj 3281(构图+网络流)的相关文章

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 (网络流之最大流)

题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到喜欢的食物和饮料? 析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点, 一个和食物连,一个和饮料连,最后跑一遍最大流即可. 代码如下: #pragma comment(linker, "/STACK:

POJ 3281 网络流dinic算法

B - Dining Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3281 Appoint description: Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she wil

POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料

如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮料层容量是1,饮料层到终点容量是1. 但是后来发现有一组hack数据: 2 3 3 3 3 1 2 3 1 2 3 3 3 1 2 3 1 2 3 我们发现一头奶牛居然吃了多个套餐,所以要解决这个只需要将自己与自己建立一条容量是1的边就行了. #include <cstdio> #include

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 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 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

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 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(最大流)

POJ 3281 Dining 题目链接 题意:n个牛,每个牛有一些喜欢的食物和饮料,每种食物饮料只有一个,问最大能匹配上多少只牛每个牛都能吃上喜欢的食物和喜欢的饮料 思路:最大流,建模源点到每个食物连一条边,容量为1,每个饮料向汇点连一条边容量为1,然后由于每个牛有容量1,所以把牛进行拆点,然后食物连向牛的入点,牛的出点连向食物,跑一下最大流即可 代码: #include <cstdio> #include <cstring> #include <queue> #in