zoukankan      html  css  js  c++  java
  • POJ 3281 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).

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

    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

    确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #define INF 0x3f3f3f3f
    #define lowbit(x) (x&(-x))
    #define eps 0.00000001
    #define pn printf("
    ")
    using namespace std;
    typedef long long ll;
    
    
    int n,f,d;
    const int MAXN = 100010;
    const int MAXM = 400010;
    struct Edge
    {
        int to,next,cap,flow; 
    }edge[MAXM];
    set <pair<int,int> > st;
    int tol;
    int head[MAXN];
    int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
    {
        tol = 0;
        memset(head,-1,sizeof(head));
    }
    //加边,单向图三个参数,双向图四个参数
    void addedge(int u,int v,int w,int rw=0) {
        edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
        edge[tol].flow = 0;head[u] = tol++;
        edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
        edge[tol].flow = 0;head[v]=tol++;
    }
    
    int sap(int start,int end,int N) {
        memset(gap,0,sizeof(gap));
        memset(dep,0,sizeof(dep));
        memcpy(cur,head,sizeof(head));
        int u = start;
        pre[u] = -1;
        gap[0] = N;
        int ans = 0;
        while(dep[start] < N)
        {
            if(u == end)
            {
                int Min = INF;
                for(int i = pre[u];i != -1; i = pre[edge[i^1].to])
                    if(Min > edge[i].cap - edge[i].flow)
                        Min = edge[i].cap - edge[i].flow;
                        for(int i = pre[u];i != -1; i = pre[edge[i^1].to])
                        {
                            edge[i].flow += Min;
                            edge[i^1].flow -= Min;
                        }
                u = start;
                ans += Min;
                continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1;i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i; break;
            }
        }
        if(flag)
        {
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1;i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u] = Min+1;
        gap[dep[u]]++;
        if(u != start) u = edge[pre[u]^1].to;
        }
        return ans;
    }
    int main()
    {
        init();
        int fi, di, x;
        cin >> n >> f >> d;
        for(int i=1;i<=f;i++) addedge(0, 2*n+i, 1);
        for(int i=1;i<=d;i++) addedge(2*n+f+i, 2*n+f+d+1, 1);
        for(int i=1;i<=n;i++) addedge(i, n+i, 1);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&fi,&di);
            for(int j=0;j<fi;j++)
            {
                scanf("%d",&x);
                addedge(2*n+x, i, 1);
            }
            for(int j=0;j<di;j++)
            {
                scanf("%d",&x);
                addedge(i+n,2*n+f+x,1);
            }
        }
        cout << sap(0, 2*n+f+d+1, 2*n+f+d+2) << endl;
    }
  • 相关阅读:
    python 包管理工具 pip 的配置
    Python 变量作用域 LEGB (下)—— Enclosing function locals
    Python 变量作用域 LEGB (上)—— Local,Global,Builtin
    2020 Java 面试题 小结 (答案慢慢补上,有错误请指出)
    mysql 根据日期(date)做年,月,日分组统计查询
    jvm指令
    正则表达式 分割地址 获取省市区详细地址
    .Net 异常记录
    WCF设计服务协议(一)
    plsql ORA-01789:查询块具有不正确的结果列数
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8622407.html
Copyright © 2011-2022 走看看