zoukankan      html  css  js  c++  java
  • B

    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.
     
     
     
     
    牛吃草问题,这个牛是一个点,有因为每一头牛只能用一次,所以要进行拆点。
    这个图很好建的,我就不说了。
     
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <cstring>
    #include <vector>
    #define inf 0x3f3f3f3f
    using namespace std;
    const int maxn = 1e5 + 10;
    int n, f, d;
    struct node
    {
        int from, to, cap, flow;
        node(int from = 0, int to = 0, int cap = 0, int flow = 0) :from(from), to(to), cap(cap), flow(flow) {}
    };
    vector<node>e;
    vector<int>G[maxn];
    int level[maxn], iter[maxn];
    void add(int u, int v, int w)
    {
        e.push_back(node(u, v, w, 0));
        e.push_back(node(v, u, 0, 0));
        int m = e.size();
        G[u].push_back(m - 2);
        G[v].push_back(m - 1);
    }
    
    void bfs(int s)//这个是为了构建层次网络,也就是level的构建
    {
        memset(level, -1, sizeof(level));
        queue<int>que;
        que.push(s);
        level[s] = 0;
        while (!que.empty())
        {
            int u = que.front(); que.pop();
            for (int i = 0; i < G[u].size(); i++)
            {
                node &now = e[G[u][i]];
                if (now.cap > now.flow&&level[now.to] < 0)//只有这个没有满并且没有被访问过才可以被访问
                {
                    level[now.to] = level[u] + 1;
                    que.push(now.to);
                }
            }
        }
    }
    
    int dfs(int u, int v, int f)
    {
        if (u == v) return f;
        for (int &i = iter[u]; i < G[u].size(); i++)
        {
            node &now = e[G[u][i]];
            if (now.cap > now.flow&&level[now.to] > level[u])
            {
                int d = dfs(now.to, v, min(f, now.cap - now.flow));
                if (d > 0)
                {
                    now.flow += d;
                    e[G[u][i] ^ 1].flow -= d;
                    return d;
                }
            }
        }
        return 0;
    }
    
    int Maxflow(int s, int t)
    {
        int flow = 0;
        while (1)
        {
            bfs(s);
            if (level[t] < 0) return flow;
            memset(iter, 0, sizeof(iter));
            int f;
            while ((f = dfs(s, t, inf) > 0)) flow += f;
        }
    }
    void init()
    {
        for (int i = 0; i <= n + 5; i++) G[i].clear();
        e.clear();
    }
    
    int main()
    {
        while (cin >> n >> f >> d)
        {
            init();
            int s = 0, t = f + 2 * n + d + 1;
            for (int i = 1; i <= f; i++) add(s, i, 1);
            for (int i = 1; i <= n; i++)
            {
                int a, b;
                cin >> a >> b;
                while (a--)//与牛i相连
                {
                    int x;
                    cin >> x;
                    add(x, f + i, 1);
                }
                add(f + i, f + n + i, 1);
                while (b--)
                {
                    int x;
                    cin >> x;
                    add(f + n + i, f + 2 * n + x, 1);
                }
            }
            for (int i = 1; i <= d; i++) add(f + 2 * n + i, t, 1);
            int ans = Maxflow(s, t);
            cout << ans << endl;
        }
        return 0;
    }
     
     
  • 相关阅读:
    WinForm 无边框窗体 拖动工作区移动窗体
    [CSS]火狐和IE对css样式解释的差异
    md类型文件迁移至Notion(img资源也可以上传)
    google推出notebook软件
    刚收到几个google analytics 邀请,有真正需要的我可以送给他一个
    最近用drupal做了一个CMS网站
    google adsense 又增加了Picasa的推介,我已加上
    googlepages空间的一个bug
    近段时间比较郁闷
    开复与学生面对面
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10798003.html
Copyright © 2011-2022 走看看