zoukankan      html  css  js  c++  java
  • POJ 3281.Dining 最大流

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18479   Accepted: 8243

    Description

    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.

    Source

    题意:有n头牛,f个食物,d个饮料。每头牛喜欢都有各自喜欢的食物和饮料,但每个食物,饮料只能分给一头牛。有多少牛可以同时得到喜欢的食物和饮料。
    思路:最大流。将牛拆分。源点——食物——牛——牛——饮料——汇点。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<set>
    using namespace std;
    #define PI acos(-1.0)
    typedef long long ll;
    typedef pair<int,int> P;
    const int maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e13+7;
    priority_queue<P,vector<P>,greater<P> >q;
    struct edge
    {
        int from,to;
        int cap;
        int rev;///方向边的编号
    };
    int n,f,d;
    vector<edge>G[maxn];
    int used[maxn];
    void addedge(int u,int v,int c)
    {
        edge e;
        e.from=u,e.to=v,e.cap=c,e.rev=G[v].size();
        G[u].push_back(e);
        e.from=v,e.to=u,e.cap=0,e.rev=G[u].size()-1;
        G[v].push_back(e);
    }
    int dfs(int u,int t,int f)
    {
        if(u==t) return f;
        used[u]=true;
        for(int i=0; i<G[u].size(); i++)
        {
            edge e=G[u][i];
            int v=e.to,c=e.cap;
            if(!used[v]&&c>0)
            {
                int d=dfs(v,t,min(f,c));
                if(d>0)
                {
                    G[u][i].cap-=d;
                    G[v][e.rev].cap+=d;
                    return d;
                }
            }
        }
        return 0;
    }
    int max_flow(int s,int t)
    {
        int flow=0;
        while(true)
        {
            memset(used,0,sizeof(used));
            int f=dfs(s,t,inf);
            if(f==0) return flow;
            flow+=f;
        }
    }
    int main()
    {
        scanf("%d%d%d",&n,&f,&d);
        for(int i=0; i<=2*n+f+d+1; i++) G[i].clear();
        for(int i=1; i<=n; i++)
        {
            int x,fi,di;
            scanf("%d%d",&fi,&di);
            for(int j=1; j<=fi; j++)
            {
                scanf("%d",&x);
                addedge(2*n+x,i,1);
            }
            for(int j=1; j<=di; j++)
            {
                scanf("%d",&x);
                addedge(n+i,2*n+f+x,1);
            }
        }
        for(int i=1; i<=n; i++) addedge(i,n+i,1);
        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);
        cout<<max_flow(0,2*n+f+d+1)<<endl;
        return 0;
    }
    最大流
  • 相关阅读:
    如何让自己的app尽量不被系统杀死
    linux常用命令-权限管理命令
    linux常用命令-用户管理命令
    linux常用命令-文件处理命令
    npm命令
    新技术新框架新工具选型原则
    tomcat启动命令行中文乱码
    docker命令
    tinkpad e450c 进入 BIOS
    基于Java服务的前后端分离解决跨域问题
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7246477.html
Copyright © 2011-2022 走看看