zoukankan      html  css  js  c++  java
  • poj3281 Dining

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 20081   Accepted: 8925

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

    Source

    题目大意:有f种食物和d种饮料,每种食物和每种饮料只能被一头牛吃/喝,每头牛都有它自己想吃/喝的,问最多能满足多少头牛的要求?
    分析:一开始感觉和二分图有点像,其实又不是二分图.和二分图很像的就是匹配的思想.那么可以利用网络流来实现匹配.
              从原点向每种食物连一条容量为1的边,对于每头牛拆成两个点,从第一个点向第二个点连一条容量为1的边,这里的目的主要是为了控制食物只能吃一种.然后食物向对应的牛的一号点连一条容量为1的边.接着牛的第二个点向对应的饮料连一条容量为1的边,最后所有饮料向汇点连边.
              拆不拆点要考虑清楚.拆点可以控制流入/流出的流量,还可以用于分层.
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 5000010,inf = 0x7fffffff;
    
    int n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],ans;
    int vis[maxn];
    
    void add(int x,int y,int z)
    {
        w[tot] = z;
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    
        w[tot] = 0;
        to[tot] = x;
        nextt[tot] = head[y];
        head[y] = tot++;
    }
    
    bool bfs()
    {
        queue <int> q;
        memset(vis,-1,sizeof(vis));
        vis[S] = 0;
        q.push(S);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            if (u == T)
                return true;
            for (int i = head[u];i;i = nextt[i])
            {
                int v = to[i];
                if (vis[v] == -1 && w[i])
                {
                    vis[v] = vis[u] + 1;
                    q.push(v);
                }
            }
        }
        return false;
    }
    
    int dfs(int u,int f)
    {
        int res = 0;
        if (u == T)
            return f;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && vis[v] == vis[u] + 1)
            {
                int temp = dfs(v,min(f - res,w[i]));
                res += temp;
                w[i] -= temp;
                w[i ^ 1] += temp;
                if (res == f)
                    return res;
            }
        }
        if (!res)
            vis[u] = -1;
        return res;
    }
    
    void dinic()
    {
        while (bfs())
           ans += dfs(S,inf);
    }
    
    int main()
    {
        scanf("%d%d%d",&n,&f,&d);
        S = 0;
        T = d + f + 2 * n + 1;
        for (int i = 1; i <= f; i++)
            add(S,i,1);
        for (int i = f + 1; i <= d + f; i++)
            add(i,T,1);
        for (int i = 1; i <= n; i++)
        {
            add(i + d + f,i + n + d + f,1);
            int a,b;
            scanf("%d%d",&a,&b);
            for (int j = 1; j <= a; j++)
            {
                int x;
                scanf("%d",&x);
                add(x,i + d + f,1);
            }
            for (int j = 1; j <= b; j++)
            {
                int x;
                scanf("%d",&x);
                add(i + n + d + f,x + f,1);
            }
        }
        dinic();
        printf("%d
    ",ans);
    
        return 0;
    }
  • 相关阅读:
    web单机优化
    html标签
    html基础
    jenkins api
    cobbler api
    Cobbler安装配置简单使用
    ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程
    在ubuntu12.04上安装6款顶级漂亮的BURG主题
    Setting up an OpenGL development environment in ubuntu
    c++ list 容器
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8137807.html
Copyright © 2011-2022 走看看