POJ - 3281(最大流+花式建图)

Dining

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

题意:有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问能使多少头牛同时获得自己喜欢的食物和饮料。

思路:只考虑一种食物的时候就非常容易,直接二分图最大匹配。而除了匈牙利算法外,网络流中的最大流算法也可以解决二分图匹配问题(通过添加s和t),所以我们可以尝试类似的方法建图求最大流。如果单单添加s和t一头羊可能会对答案贡献超过一(cow可能有多个喜欢的食物和饮料),我们可以在cow自己之间建一条容量为1的边,然后就可以套模板了。

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=500+10;
const double eps=1e-6;

int F,D,N;
struct EDGE{
    int v,c,rev;
};
vector<EDGE>G[MAX];
bool vis[MAX];

void init(){
    for(int i=1;i<=MAX;i++)
        G[i].clear();
} 

void addedge(int u,int v,int c){
    G[u].push_back((EDGE){v,c,G[v].size()});
    G[v].push_back((EDGE){u,0,G[u].size()-1});
}

int dfs(int v,int t,int f){
    if(v==t)    return f;
    vis[v]=1;
    for(int i=0;i<G[v].size();i++){
        EDGE &e=G[v][i];
        if(!vis[e.v]&&e.c>0){
            int d=dfs(e.v,t,min(f,e.c));
            if(d>0){
                e.c-=d;
                G[e.v][e.rev].c+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    while(1){
        memset(vis,0,sizeof(vis));
        int f=dfs(s,t,inf);
        if(!f)  return flow;
        flow+=f;
    }
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    while(cin>>N>>F>>D){
        init();
        int s=N*2+F+D+1;
        int t=s+1;
        for(int i=1;i<=F;i++)
            addedge(s,2*N+i,1);
        for(int i=1;i<=D;i++)
            addedge(2*N+F+i,t,1);
        for(int i=1;i<=N;i++)
            addedge(i,N+i,1);
        int x,y,T;
        for(int i=1;i<=N;i++){
            cin>>x>>y;
            for(int j=1;j<=x;j++){
                scanf("%d",&T);
                addedge(2*N+T,i,1);
            }
            for(int j=1;j<=y;j++){
                scanf("%d",&T);
                addedge(N+i,2*N+F+T,1);
            }

        }
        cout<<max_flow(s,t)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值