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

题意:匹配题,割点然后跑最大流。。。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
struct no{
    int i,j,v;
    int x[20],y[20];
};no a[1010],b[1010];
struct dd{
    int e,v,n,w;
};dd d[10010];
int top,h[1010],f[1010];
struct nn{
    int x,y,v;
};
void s0(int s,int e,int v){
    d[top].e=e;
    d[top].v=v;
    d[top].w=v;
    d[top].n=h[s];
    h[s]=top++;
}

void init(){
    top=0;
    memset(h,-1,sizeof(h));
}

int bfs(int s,int e){
    memset(f,-1,sizeof(f));
    queue<int>q;
    f[s]=0;
    q.push(s);
    while(!q.empty()){
        int p=q.front();
        q.pop();
        for(int i=h[p];i!=-1;i=d[i].n){
            int y=d[i].e;
            if(f[y]==-1&&d[i].v>0){
                f[y]=f[p]+1;
                q.push(y);
            }
        }
    }
    return f[e]!=-1;
}

int dfs(int k,int n,int v){
    if(k==n)return v;
    int v1=0,anq=0;
    for(int i=h[k];i!=-1;i=d[i].n){
        int y=d[i].e;
        if(f[y]!=f[k]+1||d[i].v<=0)continue;
        anq=dfs(y,n,min(v,d[i].v));
        if(!anq)continue;
        v-=anq;
        v1+=anq;
        d[i].v-=anq;
        d[i^1].v+=anq;
        if(!v)break;
    }
    return v1;
}

int main()
{
    int m,n1,n2;
    while(scanf("%d%d%d",&m,&n1,&n2)!=EOF){
        init();
        int k1,k2,e;
        for(int i=201;i<=200+n1;i++)
            s0(0,i,1),s0(i,0,0);
        for(int i=301;i<=300+n2;i++)
            s0(i,401,1),s0(401,i,0);
        for(int i=1;i<=m;i++){
            s0(i*2-1,i*2,1);
            s0(i*2,i*2-1,1);
            scanf("%d%d",&k1,&k2);
            for(int j=1;j<=k1;j++){
                scanf("%d",&e);
                s0(e+200,i*2-1,1),s0(i*2-1,e+200,0);
            }
            for(int j=1;j<=k2;j++){
                scanf("%d",&e);
                s0(i*2,e+300,1),s0(e+300,i*2,0);
            }
        }
        int ans=0;
        while(bfs(0,401))ans+=dfs(0,401,1e8);
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值