zoukankan      html  css  js  c++  java
  • POJ 3281 Dining(最大流)

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


    EK

    #include<map>
    #include<set>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f
    #define ll long long
    #define maxn 1000
    using namespace std;
    int n,f,d,m;
    int g[maxn][maxn];
    bool used[maxn];
    int linker[maxn];
    void net(int u,int flow){
        while(linker[u]!=-1){
            g[linker[u]][u]-=flow;
            g[u][linker[u]]+=flow;
            u=linker[u];
        }
    }
    int bfs(int s,int t){
        memset(used,false,sizeof(used));
        memset(linker,-1,sizeof(linker));
        queue<int>q;
        used[s]=true;
        int minn=inf;
        q.push(s);
        while(!q.empty()){
            int cur=q.front();
            q.pop();
            if(cur==t)break;
            for(int i=0;i<=m;i++){
                if(!used[i]&&g[cur][i]){
                    q.push(i);
                    minn=minn<g[cur][i]?minn:g[cur][i];
                    linker[i]=cur;used[i]=true;
                }
            }
        }
        if(linker[t]==-1)
        return 0;
        return minn;
    }
    int ek(int s,int t){
        int an=0,ans=0;
        do{
            an=bfs(s,t);
            ans+=an;
            net(t,an);
        }while(an!=0);
        return ans;
    }
    int main(){
        while(~scanf("%d%d%d",&n,&f,&d)){
            memset(g,0,sizeof(g));
            m=f+n+n+d+1;
            for(int i=1;i<=n;i++){
                int fi,di;scanf("%d%d",&fi,&di);
                    for(int j=1;j<=fi;j++){
                         int fii;scanf("%d",&fii);
                          g[0][fii]=1;g[fii][f+i]=1;}
                    for(int j=1;j<=di;j++){
                         int dii;scanf("%d",&dii);
                          g[f+n+i][f+2*n+dii]=1;g[f+2*n+dii][m]=1;}
            }
            for(int i=f+1;i<=f+n;i++){
                g[i][i+n]=1;
            }
            cout<<ek(0,m)<<endl;
        }
    }
    


  • 相关阅读:
    修改MySQL表中自增编号
    springboot整合mybatis统一配置bean的别名
    kotlin来了!!
    微信小程序获取登录手机号
    maven--package
    修改oracle数据库时间
    oracle启动停止命令
    安装 MySQL 之后初始密码在哪里??
    EntityFramework~~~三种模式
    webqq协议分析之~~~~验证是否需要验证码
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053276.html
Copyright © 2011-2022 走看看