POJ 3281 Dining(巧妙转化为最大流)

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19491 Accepted: 8679
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.

题意:有N头牛,F种食物和D种饮料,每头牛都有自己喜欢的食物和饮料。问你有多少头牛可以得到自己喜欢的食物和饮料

思路:
构图法如下
这里写图片描述

对于每头牛都链接他自己是为了保证每头牛都只能分配一组饮料和食物。这里所有的权值都设置为0,然后求一下源点和汇点的最大流就可以了,注意要给图上的点编号。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int MAX_V = 510;
const int INF = 0x3f3f3f3f;
struct Edge{
    int to,cap,rev;
};
vector<Edge> G[MAX_V];
int level[MAX_V],iter[MAX_V];
bool book[MAX_V][MAX_V];

void add(int u,int v,int cap){
    G[u].push_back((Edge){v,cap,(int)G[v].size()});
    G[v].push_back((Edge){u,0,(int)G[u].size()-1});
}

void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> q;
    level[s] = 0;q.push(s);
    while(!q.empty()){
        int v = q.front();q.pop();
        for(int i=0;i<G[v].size();i++){
            Edge &e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0){
                level[e.to] = level[v] + 1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int s,int t,int f){
    if(s == t)  return f;
    for(int &i = iter[s];i < G[s].size();i++){
        Edge &e = G[s][i];
        if(e.cap > 0 && level[e.to] > level[s]){
            int d = dfs(e.to,t,min(f,e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int solve(int s,int t){
    int res = 0;
    while(true){
        bfs(s);
        if(level[t] < 0)    return res;
        memset(iter,0,sizeof(iter));
        int f;
        while((f = dfs(s,t,INF)) > 0){
            res += f;
        }
    }
}
int main(void){
    int N,F,D;
    scanf("%d %d %d",&N,&F,&D);
    int fn,dn,x;
    int s = 0,t = F+N*2+D+1;
    for(int i=1;i<=N;i++){
        scanf("%d %d",&fn,&dn);
        for(int j=1;j<=fn;j++){
            scanf("%d",&x);
            if(!book[s][x]){//防止每条边被链接两次
                add(s,x,1);
                book[s][x] = true;
            }
            if(!book[x][F+i]){
                add(x,F+i,1);
                book[x][F+i] = true;
            }
        }
        add(F+i,F+N+i,1);
        for(int j=1;j<=dn;j++){
            scanf("%d",&x);
            //给图进行编号,对于左边的N1都是F+i,右边的是F+N+i
            if(!book[F+N+i][F+N+N+x]){
                add(F+N+i,F+N+N+x,1);
                book[F+N+i][F+N+N+x] = true;
            }
            if(!book[F+N+N+x][t]){
                add(F+N+N+x,t,1);
                book[F+N+N+x][t] = true;
            }
        }
    }
    printf("%d\n",solve(s,t));

    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值