Dining POJ - 3281(最大流)

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25452   Accepted: 11183

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

题意:农场有N头牛,每头牛都有要吃的食物和要喝的饮料,农夫准备了F种食物和D种饮料,第一行输入N,F,D,第2到N+1行每行输入Fi和Di,后面接着Fi个数和Di个数,代表第i头牛能吃Fi种食物,喝Di种饮料,接着的Fi个数代表食物号,Di个数代表饮料号,一种食物和饮料只有一个,问怎么分配可以使最多数量的牛满足它们的要求(即吃到能吃的食物并喝到能喝的饮料)。

题解:建图方法,将牛拆分为两个点,之间建边流量为1。  源点->食物->牛1->牛2->饮料->汇点

ps:下面的dinic一开始没有反向边建0在poj上wa了。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define LL long long
#define add addedge
#define infw in
#define ofw out
#define inf INF
#define mp edge
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const LL mod =  998244353;
int N,F,D;

struct Edge{
    int u,v,cap,next;
}edge[MAXN];
int pre[MAXN];
int dis[MAXN],cur[MAXN];
int cnt;
int sp,tp;

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

void addedge(int u,int v,int w){
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = w;
    edge[cnt].next = pre[u];
    pre[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = 0;
    edge[cnt].next = pre[v];
    pre[v] = cnt ++;
}

bool bfs(){
    memset(dis,-1,sizeof dis);
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(sp);
    dis[sp] = 0;
    int u,v;
    while(!que.empty()){
        u = que.front();
        que.pop();
        for(int i = pre[u]; i != -1; i = edge[i].next){
            v = edge[i].v;
            if(dis[v] == -1 && edge[i].cap > 0){
                dis[v] = dis[u] + 1;
                que.push(v);
                if(v == tp)
                    break;
            }
        }
    }
    return dis[tp] != -1;
}

int dfs(int u,int cap){
    if(u == tp || cap == 0) return cap;
    int res = 0,f;
    for(int i = cur[u]; i != -1 ; i = edge[i].next){
        int v = edge[i].v;
        if(dis[v] == dis[u] + 1 && (f = dfs(v,min(cap -  res,edge[i].cap))) > 0){
            edge[i].cap -= f;
            edge[i ^ 1].cap += f;
            res += f;
            if(res == cap)
                return cap;
        }
    }
    if(!res)
        dis[u] = -1;
    return res;
}
int dinic(){
    int ans = 0;
    while(bfs()){
        for(int i = sp ; i <= tp ;i++)
            cur[i] = pre[i];
        ans += dfs(sp,INF);
    }
    return ans;
}
int main()
{
    sp = 0;
    tp = 500;
    init();
    while(~scanf("%d %d %d",&N,&F,&D)) {
        int ff[MAXN], dd[MAXN];
        for (int i = 1; i <= F; i++){
            addedge(sp, i + 100, 1);
            addedge(i + 100,sp,0);
        }
        for (int i = 1; i <= D; i++) {
            addedge(i + 200, tp, 1);
            addedge(tp, i + 200, 0);
        }
        for (int i = 1; i <= N; i++) {
            addedge(i, i + 300, 1);
            addedge(i + 300,i,0);
            int nf, nd;
            scanf("%d %d", &nf, &nd);
            int tmp;
            for (int j = 0; j < nf; j++) {
                scanf("%d", &tmp);
                addedge(tmp + 100, i, 1);
                addedge(i,tmp + 100,0);
            }
            for (int j = 0; j < nd; j++) {
                scanf("%d", &tmp);
                addedge(i + 300, tmp + 200, 1);
                addedge(tmp + 200,i + 300,0);
            }
        }
        int ans = dinic();
        printf("%d\n", ans);
    }
}

原文地址:https://www.cnblogs.com/smallhester/p/11252164.html

时间: 2024-10-08 20:17:23

Dining POJ - 3281(最大流)的相关文章

poj 3281 最大流+建图

很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很妙! 题意就是有N头牛,F个食物,D个饮料. N头牛每头牛有一定的喜好,只喜欢几个食物和饮料. 每个食物和饮料只能给一头牛.一头牛只能得到一个食物和饮料. 而且一头牛必须同时获得一个食物和一个饮料才能满足.问至多有多少头牛可以获得满足. 最初相当的是二分匹配.但是明显不行,因为要分配两个东西,两个东

poj 3281 最大流拆点

Language: Default Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10321   Accepted: 4744 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John

AC日记——Dining poj 3281

[POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 2005 #define INF 0x7fffffff int n,f,

POJ 3281 /// 最大流

题目大意: n 头牛 f 种食物 d 种饮料 每头牛有各自喜欢的食物和饮料 求最多有多少头牛能分配到自己喜欢的食物和饮料 因为同时有食物和饮料 所以不能用二分图匹配 用最大流解决二分图匹配的办法 增加一个源点连向所有食物 每头牛与各自喜欢的食物连边 增加一个汇点连向所有的饮料 每头牛与各自喜欢的饮料连边 以上边容量都为1 单纯这样连的话 一头牛可能分配到多种食物和饮料 把一头牛拆成两个点 一点与食物连边 另一点与饮料连边 再在两个点之间连一条容量为1的边 这样就能保证只有一个流量流过 即只有一种

B - 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

kuangbin专题专题十一 网络流 Dining POJ - 3281

题目链接:https://vjudge.net/problem/POJ-3281 题目:有不同种类的食物和饮料,每种只有1个库存,有N头牛,每头牛喜欢某些食物和某些饮料,但是一头牛 只能吃一种食物和喝一种饮料,问怎么分配食物和饮料才能让最多数量的牛饱餐. 思路:容易想到  食物->牛->饮料的流,当然一个牛可以被多个饮料流到,需要把牛拆成入点和出点,入点和出点流量为1,这样可以保证牛只吃或者喝某种食物和饮料,别的都流是套路,除了牛的分点之间流量为1,别的连接设置成1或者INF都一样,因为有牛的

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

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(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

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