zoukankan      html  css  js  c++  java
  • poj 3281

    Language:
    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11719   Accepted: 5387

    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: 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 Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow iwill 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.

    Source

    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 88888*4;
    const int oo = 0x3f3f3f3f;
    int head[N], start, End, cnt, vis[N];
    struct da
    {
        int v, flow, next;
    } as[N];
    void add(int u, int v, int flow)
    {
        as[cnt].v = v;
        as[cnt].flow = flow;
        as[cnt].next = head[u];
        head[u] = cnt++;
    }
    int bfs()
    {
        queue<int >Q;
        memset(vis, 0, sizeof(vis));
        vis[start] = 1;
        Q.push(start);
        while(!Q.empty())
        {
           int u = Q.front();
           Q.pop();
           if(u == End) return 1;
           for(int j = head[u]; j != -1; j = as[j].next)
           {
               int v = as[j].v;
               if(!vis[v] && as[j].flow)
               {
                   vis[v] = vis[u] + 1;
                   Q.push(v);
               }
           }
        }
        return 0;
    }
    int dfs(int u, int Maxflow)
    {
        int v, flow, uflow=0;
        if(u == End) return Maxflow;
        for(int j = head[u]; j != -1; j = as[j].next)
        {
            v = as[j].v;
            if(vis[v] == vis[u]+1 && as[j].flow)
            {
                flow = min(as[j].flow, Maxflow-uflow);
                flow = dfs(v, flow);
                as[j].flow -= flow;
                as[j^1].flow += flow;
                uflow += flow;
                if(uflow == Maxflow)break;
            }
        }
        if(uflow == 0)vis[u] = 0;
        return uflow;
    }
    int dinic()
    {
        int ans = 0;
        while(bfs())
            ans += dfs(start, oo);
        return ans;
    }
    int main()
    {
        int N, F, D, i, j, num, ans, fi, di;
        while(~scanf("%d %d %d", &N, &F, &D))
        {
            memset(head, -1, sizeof(head));
            cnt = 0;
            start = 0;
            End = F+D+2*N+2;
            for(i = 1; i <= N; i++)
            {
                scanf("%d %d", &fi, &di);
                for(j = 1; j <= fi; j++)
                {
                    scanf("%d", &num);
                    add(num, F+i, 1);
                    add(F+i, num, 0);
                }
                for(j = 1; j <= di; j++)
                {
                    scanf("%d", &num);
                    add(F+i+N, F+2*N+num, 1);
                    add(F+2*N+num, F+N+i, 0);
                }
                add(F+i, F+N+i, 1);
                add(F+N+i, F+i, 0);
            }
            for(i = 1; i <= F; i++)
            {
                add(start, i, 1);
                add(i, start, 0);
            }
            for(i = 1; i <= D; i++)
            {
                add(2*N+F+i, End, 1);
                add(End, 2*N+F+i, 0);
            }
            ans = dinic();
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    IT小小鸟读书笔记2
    第五周读书笔记
    JSON Schema 入门指南【Java后端使用】
    win10装多个MySQL(MySQL 8.0免安装版)
    记一些实习生问我的问题
    JAVA项目(maven)使用钉钉SDK(获取token、用户等)
    从项目开始的前端开发学习
    从项目开始的Java开发学习
    HBuilderX 5+APP MUI 入门
    项目部署各种配置
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4743469.html
Copyright © 2011-2022 走看看