POJ 3281 Dining

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

题意:有N头牛,F种食物,D种饮料,每头牛有一些喜欢的食物和饮料,但每种饮料和每种食物只能给一头牛,一头牛也只能吃一种饮料和一种食物,问有多少头牛可以既吃到自己喜欢的食物又吃到自己喜欢的饮料。

就是一道网络流的水题。。
但要注意每头牛只能吃一种食物和一种饮料,所以不能直接将食物连向牛,再把连向饮料,应该先把牛拆点,中间连一条流量为1的边来限制每只牛只能选一种。
所以最后构图就成了源点-食物-牛1-牛2-饮料-汇点。
流量都是1。(虽然我做的时候有的流量是INF 23333)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 100010;
const int INF = 1<<30;

int n,f,d;
int s,t;

struct node{
    int pre,v,f;
}edge[N];

int num=1;
int head[N];

void addedge(int from,int to,int f){
    num++;
    edge[num].pre=head[from];
    edge[num].v=to;
    edge[num].f=f;
    head[from]=num;
    num++;
    edge[num].pre=head[to];
    edge[num].v=from;
    edge[num].f=0;
    head[to]=num;
}

int dis[N],state[N];
bool vis[N];
bool bfs(){
    int h=0,tail=1;
    state[1]=s,dis[s]=0,vis[s]=1;
    do{
        h++;
        int u=state[h];
        for(int i=head[u];i;i=edge[i].pre){
            int v=edge[i].v;
            if(!vis[v]&&edge[i].f){
                vis[v]=true;
                dis[v]=dis[u]+1;
                tail++;
                state[tail]=v;
            }
        }
    }
    while(h<tail);
    if(dis[t]) return true;
    return false;
}

inline int Min(int a,int b){
    return a<b?a:b;
}

int dfs(int u,int delta){
    if(u==t||delta==0) return delta;
    int ans=0;
    for(int i=head[u];i&&delta;i=edge[i].pre){
        int v=edge[i].v;
        if(dis[v]==dis[u]+1&&edge[i].f){
            int dd=dfs(v,Min(delta,edge[i].f));
            edge[i].f-=dd;
            edge[i^1].f+=dd;
            delta-=dd;
            ans+=dd;
        }
    }
    if(!ans) dis[u]=-1;
    return ans;
}

void zero(){
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(state,0,sizeof(state));
}

int Maxflow(){
    int ans=0;
    while(1){
        zero();
        if(!bfs()) break;
        ans+=dfs(s,INF);
    }
    return ans;
}

int main(){
    scanf("%d%d%d",&n,&f,&d);
    s=0,t=2*n+f+d+1;
    for(int i=1;i<=n;i++){
        int fi,di;
        addedge(i,n+i,1);
        scanf("%d%d",&fi,&di);
        for(int j=1;j<=fi;j++){
            int x;
            scanf("%d",&x);
            addedge(2*n+x,i,INF);
        }
        for(int j=1;j<=di;j++){
            int x;
            scanf("%d",&x);
            addedge(i+n,2*n+f+x,INF);
        }
    }
    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);
    printf("%d",Maxflow());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值