zoukankan      html  css  js  c++  java
  • 寒假Day17POJ3281Dining(最大流+多对一的匹配)

    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.
     
     
     

    AC代码:(不好写)bfs+dfs+dinic

      1 #include<string.h>
      2 #include<iostream>
      3 #include<stdio.h>
      4 #include<algorithm>
      5 #include<queue>
      6 #include<vector>
      7 #include<map>
      8 #include<cmath>
      9 using namespace std;
     10 #define inf 0x3f3f3f3f
     11 //const int N=20020;
     12 const int N=1100;
     13 
     14 typedef long long ll;
     15 
     16 int n,m,s,t,tot;
     17 int head[N],nextt[N],dep[N],cur[N];
     18 int f[N][N],d[N][N],e[N][N];
     19 
     20 int bfs()//重新建图、进行分层建图
     21 {
     22     queue<int>Q;
     23     memset(dep,-1,sizeof(dep));//这里注意一下是mem还是for清空,小心超时
     24     dep[s]=1;//dep[s]=1; 相当于给源点分层为1
     25     Q.push(s);//Q.push(s);
     26     while(!Q.empty())
     27     {
     28         int u=Q.front();
     29         Q.pop();
     30         for(int i=1; i<=t; i++)
     31         {
     32             //如果可以到达且还没有访问则入队
     33             //可以到达的条件是剩余容量大于0
     34             //没有访问的条件是当前层数==-1
     35             if(e[u][i]>0&&dep[i]==-1)
     36             {
     37                 dep[i]=dep[u]+1;
     38                 Q.push(i);
     39             }
     40         }
     41     }
     42     return dep[t]!=-1;
     43 }
     44 
     45 int dfs(int u,int minflow)//u起点(第一次传进来是1,之后会变)/查找路径上的最小流量
     46 {
     47     if(u==t)
     48         return minflow;
     49     int x;
     50     for(int v=1; v<=t; v++)
     51     {
     52         if(e[u][v]>0&&dep[v]==dep[u]+1&&(x=dfs(v,min(minflow,e[u][v]))))
     53             // if(e[u][v]>0&&dep[v]==dep[u]+1)
     54 
     55         {
     56             // x=dfs(v,min(minflow,e[u][v]));
     57             e[u][v]-=x;
     58             e[v][u]+=x;
     59             return x;
     60         }
     61     }
     62     return 0;//一定要写!!!
     63 }
     64 
     65 
     66 
     67 int dinic()
     68 {
     69     int sum=0;
     70     while(bfs())//先进行分层
     71     {
     72         while(1)
     73         {
     74             int w=dfs(s,inf);
     75             if(w==0)
     76                 break;//找不到增广路了
     77             sum+=w;//找到则进行累加
     78         }
     79     }
     80     return sum;
     81 }
     82 
     83 int main()
     84 {
     85     int F,D;
     86     while(~scanf("%d %d %d",&n,&F,&D))
     87     {
     88         memset(e,0,sizeof(nextt));
     89         for(int i=1; i<=n; i++)
     90         {
     91             int ff,dd;
     92             scanf("%d %d",&ff,&dd);
     93             for(int j=1; j<=ff; j++)
     94                 scanf("%d",&f[i][j]);
     95             for(int j=1; j<=dd; j++)
     96                 scanf("%d",&d[i][j]);
     97         }
     98         //s-f、f-牛、牛-牛、牛-d、d-t
     99         //0 4 3 2 -> 0 1-4 5-7 8-9
    100         //s:0           f:1-F           牛:F+1,F+n
    101         //牛:F+n+1,F+2n d:F+2n+1,F+2n+D  t:F+2n+D+1
    102 
    103         s=0,t=F+2*n+D+1;
    104         for(int i=1; i<=F; i++) //s-f
    105             //add(s,i,1);//inf??
    106             e[s][i]=1;
    107         for(int i=1; i<=n; i++) //枚举牛,可以少些一些for循环
    108         {
    109             for(int j=1; j<=F; j++)
    110             {
    111                 if(f[i][j])
    112                     // add(j,i+F,1);
    113                     // add(f[i][j],i+F,1);
    114                     e[f[i][j]][i+F]=1;
    115             }
    116             //add(F+i,F+n+j,1);
    117             e[F+i][F+n+i]=1;
    118             for(int j=1; j<=D; j++)
    119             {
    120                 if(d[i][j])
    121                     e[F+n+i][F+2*n+d[i][j]]=1;
    122                 //add(F+n+i,F+2*n+i,1);
    123                 //add(F+n+i,F+2*n+d[i][j],1);
    124             }
    125         }
    126         for(int i=1; i<=D; i++)
    127             e[F+2*n+i][t]=1;
    128         //add(F+2*n+i,t,1);
    129         int ans=dinic();
    130         printf("%d\n",ans);
    131     }
    132     return 0;
    133 }
    View Code
  • 相关阅读:
    VUE学习日记(十八) ---- 传递数据
    VUE学习日记(十七) ---- 组件数据函数
    VUE学习日记(十六) ---- 表行组件
    DataGridView控件使用Demo
    C# ADO.NET连接字符串详解
    SQL Server management studio使用sa连接时报错与伺服器的连接已成功,但在登入程序是发生错误
    Oracle Rac to Rac One Node
    online创建索引中途取消导致索引无法删除解决办法
    oracle常用hint添加
    C# url的编码解码,xml和json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/OFSHK/p/12250701.html
Copyright © 2011-2022 走看看