• Dining POJ


    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: NF, 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 Fiintegers 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.

    是我做题太少了

    在读完题之后完全没有发现本题到底跟网络流有什么关系

    看了网上的题解之后瞬间领悟(喔喔)

    网络流不但能解决抽象的网络问题 更能解决实际的生产生活问题

    那本题怎样建模呢

    图:源点->食物->牛->牛->饮料->汇点

    每一条边的容量全为  1

    那么从原点到达汇点的最大流就一定是所求答案了

    #include <iostream>
    #include <queue>
    #include<map>
    #include<cstring>
    #include<cstdio>
    #define MEET n*2+fo+d+1
    using namespace std;
    
    const int INF = 1e9+100;
    int level[405];
    int n,fo,d;
    int  cnt=-1;
    
    int first[405];//0 源点 1-2n 牛 2n+d food 2n+d+p drink
    int nxt[50000];
    int to[50000];
    int c[50000];
    int f[50000];
    
    bool dinic_bfs()      //bfs方法构造层次网络
    {
        //cout<<"level"<<endl;
        queue<int> q;
        memset(level, 0, sizeof(level));
        q.push(0);
        level[0] = 1;
        int u, v;
        while (!q.empty())
        {
            u = q.front();
            q.pop();
            if(u==MEET) return true;
            for (int i = first[u]; i != -1; i=nxt[i])
            {
                v=to[i];
                if (!level[v] && c[i]>f[i])
                {
                    level[v] = level[u] + 1;
                    q.push(v);
                }
            }
        }
       return false;               //question: so it must let the sink node is the Mth?/the way of yj is give the sink node's id
    }
    
    int dinic_dfs(int u, int cp)             //use dfs to augment the flow
    {
        //cout<<"cp"<<cp<<endl;
        int tmp = cp;
        int v, t;
        if (u == MEET)
            return cp;
        for (int i=first[u]; i!=-1&&tmp; i=nxt[i])
        {
            //cout<<"i3"<<i<<endl;
            v=to[i];
            if (level[u] + 1 == level[v])
            {
                if (c[i]>f[i])
                {
                    t = dinic_dfs(v, min(tmp,c[i] - f[i]));
                    f[i] += t;
                    f[i^1] -= t;
                    tmp -= t;
                }
            }
        }
        return cp - tmp;
    }
    
    int dinic()
    {
        int sum=0, tf=0;
        while (dinic_bfs())
        {
            while (tf = dinic_dfs(0, INF))
                sum += tf;
        }
        return sum;
    }
    
    void add(int st,int  en)
    {
        cnt++;
        nxt[cnt]=first[st];
        first[st]=cnt;
        to[cnt]=en;
        c[cnt]=1;
        f[cnt]=0;
    
        cnt++;
        nxt[cnt]=first[en];
        first[en]=cnt;
        to[cnt]=st;
        c[cnt]=0;
        f[cnt]=0;
    }
    
    int main()//0为起点 2n+1为终点 奇数为边的起点 偶数为边的终点
    {
        memset(first,-1,sizeof(first));
        scanf("%d%d%d",&n,&fo,&d);
        for(int i=1; i<=fo; i++) // 先建源点 和 food  在建 drink 和终点
        {
            add(0,2*n+i);
        }
        for(int i=1; i<=d; i++) // 先建源点 和 food  在建 drink 和终点
        {
            add(2*n+fo+i,MEET);
        }
        for(int i=1; i<=n; i++)
        {
            add(i*2-1,i*2);
            int tmp1,tmp2;
            int tmp;
            scanf("%d%d",&tmp1,&tmp2);
            for(int j=1; j<=tmp1; j++)
            {
                scanf("%d",&tmp);
                add(2*n+tmp,2*i-1);
            }
            for(int j=1; j<=tmp2; j++)
            {
                scanf("%d",&tmp);
                add(2*i,2*n+fo+tmp);
            }
        }
    //    for(int i=0;i<=MEET;i++)
    //    {
    //        printf("%02d :",i);
    //        for(int j=first[i];j!=-1;j=nxt[j])
    //        {
    //            printf("%3d ",to[j]);
    //        }
    //        cout<<endl;
    //    }
        int ans;
        ans=dinic();
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    吴恩达老师机器学习课程学习--课时三
    吴恩达老师机器学习课程学习--课时二
    Linux查看大文件日志
    maven导出工程pom文件中依赖的jar包
    通过生成HFile导入HBase
    hbase之RPC详解
    HBase最佳实践-读性能优化策略
    HBaseRegionServer宕机数据恢复
    HBase–RegionServer宕机恢复原理
    为什么不建议在 HBase 中使用过多的列族
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852273.html
Copyright © 2020-2023  润新知