zoukankan      html  css  js  c++  java
  • POJ

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18230   Accepted: 8132

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

    Source

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?
    其实这就是一个最大流 关键是建图
    因为题目问的是多少头牛可以得到喜欢的食物和饮料,当时建图我就按照食物-->牛-->饮料这样建图
    但存在一种牛可以满足同时得到多种自己喜欢的食物和饮料,所以这样是错误的  后来看了题解  才发现要将牛拆成两个点
    食物-->牛---->牛----->饮料   流量都为1  这样就可以保证求到的一定是最大流
    写了个前向星+Dinc的板子
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<string.h>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<cmath>
    typedef long long ll;
    typedef unsigned long long LL;
    using namespace std;
    const double PI=acos(-1.0);
    const double eps=0.0000000001;
    const int INF=1e9;
    const int N=1000000+100;
    int vis[1000][1000];
    int dis[N];
    int head[N];
    int a[N],b[N];
    int n,f,d;
    int tot;
    struct node{
        int to,next,flow,c;
    }edge[N<<1];
    void init(){
        memset(head,-1,sizeof(head));
        tot=0;
    }
    void add(int u,int v,int c){
        edge[tot].to=v;
        edge[tot].flow=c;
        edge[tot].next=head[u];
        head[u]=tot++;
        edge[tot].to=u;
        edge[tot].flow=0;
        edge[tot].next=head[v];
        head[v]=tot++;
    
    }
    int BFS(int s,int t){
        queue<int>q;
        memset(dis,-1,sizeof(dis));
        dis[s]=0;
        q.push(s);
        while(!q.empty()){
            int x=q.front();
            q.pop();
            if(x==t)return 1;
            for(int i=head[x];i!=-1;i=edge[i].next){
                int v=edge[i].to;
                if(edge[i].flow&&dis[v]==-1){
                    dis[v]=dis[x]+1;
                    q.push(v);
                }
            }
        }
        if(dis[t]==-1)return 0;
        return 1;
    }
    int DFS(int s,int flow){
        if(s==2*n+d+f+1)return flow;
        int ans=0;
        for(int i=head[s];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(edge[i].flow&&dis[v]==dis[s]+1){
                int f=DFS(v,min(flow-ans,edge[i].flow));
                edge[i].flow-=f;
                edge[i^1].flow+=f;
                ans+=f;
                if(ans==flow)return ans;
            }
        }
        return ans;
    }
    int Dinc(int s,int t){
        int flow=0;
        while(BFS(s,t)){
            //cout<<1<<endl;
            flow+=DFS(s,INF);
            //cout<<flow<<endl;
        }
        return flow;
    }
    int main(){
        while(scanf("%d%d%d",&n,&f,&d)!=EOF){
            init();
            int s=0;
            for(int i=1;i<=f;i++){
                add(s,2*n+i,1);
            }
            for(int i=1;i<=d;i++){
                add(2*n+f+i,2*n+f+d+1,1);
            }
            for(int i=1;i<=n;i++){
                add(i,i+n,1);
            }
            int D,F;
            for(int i=1;i<=n;i++){
                scanf("%d%d",&D,&F);
                for(int j=1;j<=D;j++){
                    scanf("%d",&a[j]);
                    add(2*n+a[j],i,1);
                }
                for(int j=1;j<=F;j++){
                    scanf("%d",&b[j]);
                    add(n+i,2*n+f+b[j],1);
                }
            }
            cout<<Dinc(s,2*n+f+d+1)<<endl;
        }
    
    }
  • 相关阅读:
    解决.net core 3.1 json日期带T的问题
    C# 将指定得时间转换成秒
    WPF Button按钮的按下和抬起分别触发的事件
    WPF Image图片后台绑定图片,不对图片照成占用
    WPF Textbox漂亮的文本控件框封装
    WPF Slider实现可拖动的时间轴
    .net mvc 下显示PDF文件
    .net core 3.1使用命令自动生成MySQL表对应的实体类
    .net core A generic error occurred in GD 已解决
    WPF 模拟Button按钮事件触发
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/7205241.html
Copyright © 2011-2022 走看看