zoukankan      html  css  js  c++  java
  • Dining poj3281

    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
    

    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

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<set>
    using namespace std;
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1;
        char c=getchar();
        for(; c<'0'||c>'9'; c=getchar())
            if(c=='-')
                f=-1;
        for(; c>='0'&&c<='9'; c=getchar())
            x=(x<<1)+(x<<3)+(c&15);
        x*=f;
    }
    template<typename T>inline void print(T x)
    {
        if(x<0)
            putchar('-'),x*=-1;
        if(x>=10)
            print(x/10);
        putchar(x%10+'0');
    }
    int N,F,D;
    const int MN=1e3+10;
    const int inf=0x3f3f3f3f;
    bool likeF[MN][MN];
    bool likeD[MN][MN];
    bool r[MN][MN];
    int pre[MN];
    bool visit[MN];
    typedef long long ll;
    bool BFS(int s,int t)
    {
        int p;
        queue<int>q;
        memset(pre,-1,sizeof(pre));
        memset(visit,false,sizeof(visit));
        pre[s]=s;
        visit[s]=true;
        q.push(s);
        while(!q.empty())
        {
            p=q.front();
            q.pop();
            for(int i=0; i<=2*N+F+D+1; i++)
            {
                if(r[p][i]>0&&!visit[i])
                {
                    pre[i]=p;
                    visit[i]=true;
                    if(i==t)
                        return true;
                    q.push(i);
                }
            }
        }
        return false;
    }
    ll max_flow(int s,int t)
    {
        int flow=0,d,i;
        while(BFS(s,t))
        {
            d=inf;
            for(i=t; i!=s; i=pre[i])
            {
                d=d<r[pre[i]][i]?d:r[pre[i]][i];
                for(i=t; i!=s; i=pre[i])
                {
                    r[pre[i]][i]-=d;
                    r[i][pre[i]]+=d;
                }
            }
            flow+=d;
        }
        return flow;
    }
    void solve()
    {
        int s=N*2+F+D,t=s+1;
        for(int i=0; i<F; i++)
            r[s][N*2+i]=1;
        for(int i=0; i<D; i++)
            r[2*N+F+i][t]=1;
        for(int i=0; i<N; i++)
        {
            r[i][i+N]=1;
            for(int j=0; j<F; j++)
            {
                if(likeF[i][j])
                    r[2*N+j][i]=1;
            }
            for(int j=0; j<D; j++)
            {
                if(likeD[i][j])
                    r[N+i][2*N+j+F]=1;
            }
        }
        printf("%d
    ",max_flow(s,t));
    }
    int main()
    {
        read(N),read(F),read(D);
        int a,b,c;
        for(int i=0; i<N; i++)
        {
            read(a),read(b);
            while(a--)
            {
                read(c);
                likeF[i][c-1]=1;
            }
            while(b--)
            {
                read(c);
                likeD[i][c-1]=1;
            }
        }
        solve();
    }
    
    

    思路

    应该关键在建图吧。

    建好图之后求最大流。 (这里用了EK,复习一下吧)

    PS

    一定把图建准确哦。

  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/xxffxx/p/11969943.html
Copyright © 2011-2022 走看看