zoukankan      html  css  js  c++  java
  • 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.
     
    很巧妙的建图的方法,建立s->food->cow1->cow2->drink->T的图,然后跑最大流即可,S到food以及drink到T的流量设为1,这样保证每种食物和饮料只被使用一次,且如果我们完成一次流量为1的流,一定经过一个drink和food.
    然后我们需要进行拆点,拆点的对象是牛,因为如果我们只把每一头牛作为一个点,那么它在跑最大流的时候,可能同一个牛会被多次使用,所以我们需要进行拆点,将牛的点分为cow1和cow2,且它们之间的流量为1.这样就可以解决这个问题
    并且,题目中是两种物品的挑选,我们可以类推到如果是三种,甚至更多物品的挑选,我们可以采用类似的方法,只需要将除了源点和汇点连接的对象之外都进行拆点操作即可。
     
    代码如下:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int n,f,d,b,c,x,n1,n2;
    struct Edge{
         int to,Next,flow;
    }edge[100010];
    int first[17000],size1;
    int dl[17100],deep[17100];
    void add_edge(int x,int y,int z){
        size1++;
        edge[size1].to=y;
        edge[size1].flow=z;
        edge[size1].Next=first[x];
        first[x]=size1;
    }
    
    bool bfs(int S,int T)
    {
         int head=0,tail=1;
         memset(deep,0,sizeof(deep));
         deep[S]=1,dl[1]=S;
         while(head!=tail){
            int v=dl[++head];
            for(int u=first[v];u;u=edge[u].Next){
                if(!deep[edge[u].to]&&edge[u].flow>0){
                    deep[edge[u].to]=deep[v]+1;
                    dl[++tail]=edge[u].to;
                }
            }
         }
         return deep[T];
    }
    
    int dfs(int now,int aim,int flow){
        if(now==aim)return flow;
        int F=0;
        for(int u=first[now];u&&flow;u=edge[u].Next){
            if(deep[edge[u].to]==deep[now]+1&&edge[u].flow){
                int ret=dfs(edge[u].to,aim,min(flow,edge[u].flow));
                F+=ret;flow-=ret;edge[u].flow-=ret,edge[u^1].flow+=ret;
            }
        }
        if(!F)deep[now]=-2;
        return F;
    }
    
    int maxflow(int S,int T){
          int ret=0;
          while(bfs(S,T)){
              ret+=dfs(S,T,(1<<30));
          }
          return ret;
    }
    
    int S,T;
    int main()
    {
        while(scanf("%d%d%d",&n,&f,&d)!=EOF)
        {
        memset(first,0,sizeof(first));
        S=0,T=f+n*2+d+1;
        size1=1;
        for(int i=1;i<=n;i++)
        {
          scanf("%d%d",&n1,&n2);
          for(int k=1;k<=n1;k++)
          {
             scanf("%d",&x);
             add_edge(x,f+i*2-1,1);
             add_edge(f+i*2-1,x,0);
          }
          add_edge(f+i*2-1,f+i*2,1);
          add_edge(f+i*2,f+i*2-1,0);
          for(int k=1;k<=n2;k++)
          {
              scanf("%d",&x);
             add_edge(f+i*2,f+n*2+x,1);
             add_edge(f+n*2+x,f+i*2,0);
          }
        }
         for(int i=1;i<=f;i++)
         {
             add_edge(S,i,1);
             add_edge(i,S,0);
         }
         for(int i=1;i<=d;i++)
         {
             add_edge(f+n*2+i,T,1);
             add_edge(T,f+n*2+i,0);
         }
         printf("%d
    ",maxflow(S,T));
        }
        return 0;
    }
     
  • 相关阅读:
    linux给用户添加sudo权限
    VirtualBox下安装ubuntu图文教程以及软件安装
    线程池代码(通用版)
    linux下配置jdk+tomcat
    线程池代码(加强版)
    线程池的理解与简单实现(学习版)
    条件变量pthread_cond_wait()和pthread_cond_signal()详解
    五种编程模型
    Linux线程退出、资源回收、资源清理的方法
    数据结构
  • 原文地址:https://www.cnblogs.com/a249189046/p/9781400.html
Copyright © 2011-2022 走看看