• TZOJ 1705 Dining(拆点最大流)


    描述

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

    输入

    Line 1: Three space-separated integers: N, F, 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.

    输出

    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

    样例输入

    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

    样例输出

    3

    提示

    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.
    题意
    N头牛,每头牛有F个喜欢的食物,D个喜欢的饮料,每种食物每瓶饮料只能供一头牛,问最多几头牛能感到满足(即又有喜欢的食物也有喜欢的饮料)
    题解
    首先食物连源点S流量1,饮料连汇点T流量1,食物连喜欢的牛流量1,饮料也连喜欢的牛流量1
    这时候有个问题就是牛只能流出最多1的流量和饮料匹配,所以我们可以把牛拆成左牛和右牛,这样保证从牛最多流出1的流量
    那么食物连左牛流量1,左牛连右牛流量1,右牛连饮料流量1
    建完图,跑最大流算法即可
    代码
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int N=1e5+5;
     5 const int M=2e5+5;
     6 int n,m,S,T;
     7 int deep[N],q[400000];
     8 int FIR[N],TO[M],CAP[M],COST[M],NEXT[M],tote;
     9 
    10 void add(int u,int v,int cap)
    11 {
    12     TO[tote]=v;
    13     CAP[tote]=cap;
    14     NEXT[tote]=FIR[u];
    15     FIR[u]=tote++;
    16 
    17     TO[tote]=u;
    18     CAP[tote]=0;
    19     NEXT[tote]=FIR[v];
    20     FIR[v]=tote++;
    21 }
    22 bool bfs()
    23 {
    24     memset(deep,0,sizeof deep);
    25     deep[S]=1;q[1]=S;
    26     int head=0,tail=1;
    27     while(head!=tail)
    28     {
    29         int u=q[++head];
    30         for(int v=FIR[u];v!=-1;v=NEXT[v])
    31         {
    32             if(CAP[v]&&!deep[TO[v]])
    33             {
    34                 deep[TO[v]]=deep[u]+1;
    35                 q[++tail]=TO[v];
    36             }
    37         }
    38     }
    39     return deep[T];
    40 }
    41 int dfs(int u,int fl)
    42 {
    43     if(u==T)return fl;
    44     int f=0;
    45     for(int v=FIR[u];v!=-1&&fl;v=NEXT[v])
    46     {
    47         if(CAP[v]&&deep[TO[v]]==deep[u]+1)
    48         {
    49             int Min=dfs(TO[v],min(fl,CAP[v]));
    50             CAP[v]-=Min;CAP[v^1]+=Min;
    51             fl-=Min;f+=Min;
    52         }
    53     }
    54     if(!f)deep[u]=-2;
    55     return f;
    56 }
    57 int maxflow()
    58 {
    59     int ans=0;
    60     while(bfs())
    61         ans+=dfs(S,1<<30);
    62     return ans;
    63 }
    64 void init()
    65 {
    66     tote=0;
    67     memset(FIR,-1,sizeof FIR);
    68 }
    69 int main()
    70 {
    71     int v,cow,F,D,food,drink;
    72     init();
    73     cin>>cow>>F>>D;
    74     S=F+2*cow+D+1,T=S+1;
    75     for(int i=1;i<=F;i++)
    76         add(S,i,1);
    77     for(int i=F+2*cow+1;i<=F+2*cow+D;i++)
    78         add(i,T,1);
    79     for(int i=F+1;i<=F+cow;i++)
    80     {
    81         add(i,cow+i,1);
    82         cin>>food>>drink;
    83         while(food--)cin>>v,add(v,i,1);
    84         while(drink--)cin>>v,add(cow+i,F+2*cow+v,1);
    85     }
    86     cout<<maxflow();
    87     return 0;
    88 }
  • 相关阅读:
    【class2src】Decompiler
    【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)
    【IBM-WALA】Step by Step : use WALA to generate System Dependency Graph PDF and Dot File (Mac)
    [R]统计工具包
    [PYTHON-TSNE]可视化Word Vector
    【Latex】常用工具包
    [Python]编程之美
    【python】用python生成pdf文件
    【python】并查集
    【Scikit】实现Multi-label text classification代码模板
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9535513.html
Copyright © 2020-2023  润新知