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;
    }
  • 相关阅读:
    Matplotlib
    【源码解读】EOS测试插件:txn_test_gen_plugin.cpp
    EOS多节点组网:商业场景分析以及节点启动时序
    EOS商业落地利器:多签名操作与应用
    EOS技术研究:合约与数据库交互
    【精解】EOS标准货币体系与源码实现分析
    【精解】EOS智能合约演练
    Efficient&Elegant:Java程序员入门Cpp
    区块链3.0:拥抱EOS
    以太坊挖矿源码:clique算法
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8622407.html
Copyright © 2011-2022 走看看