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

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
         

    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

    分析
    之前做过类似的题了,建图就是将奶牛拆成两个点,左边连食物,右边连饮料,食物和饮料分别连源点和汇点,跑最大流就行了。
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    using namespace std;
    //****************************************************
    //最大流模板Edmonds_Karp算法
    //初始化:G[][],st,ed
    //******************************************************
    const int MAXN = 500;
    const int INF = 0x3fffffff;
    int G[MAXN][MAXN];//存边的容量,没有边的初始化为0
    int path[MAXN],flow[MAXN],st,ed;
    int n;//点的个数,编号0~n,n包括了源点和汇点
    
    queue<int>q;
    int bfs()
    {
        int i,t;
        while(!q.empty()) q.pop();//清空队列
        memset(path,-1,sizeof(path));//每次搜索前都把路径初始化成-1
        path[st]=0;
        flow[st]=INF;//源点可以有无穷的流流进
        q.push(st);
        while(!q.empty()){
            t=q.front();
            q.pop();
            if(t==ed) break;
            for(i=0;i<=n;i++){
                if(i!=st&&path[i]==-1&&G[t][i]){
                    flow[i]=flow[t]<G[t][i]?flow[t]:G[t][i];
                    q.push(i);
                    path[i]=t;
                }
            }
        }
        if(path[ed]==-1) return -1;//即找不到汇点上去了。找不到增广路径了
        return flow[ed];
    }
    
    int Edmonds_Karp()
    {
        int max_flow=0;
        int step,now,pre;
        while((step=bfs())!=-1){
            max_flow+=step;
            now=ed;
            while(now!=st){
                pre=path[now];
                G[pre][now]-=step;
                G[now][pre]+=step;
                now=pre;
            }
        }
        return max_flow;
    }
    
    int main()
    {
        int N,F,D;
        memset(G,0,sizeof(G));
        scanf("%d%d%d",&N,&F,&D);
        n=2*N+F+D+1;
        st=0,ed=n;
        for(int i=1;i<=F;i++) G[st][i]=1;
        for(int i=1;i<=N;i++) G[F+i][N+F+i]=1;
        for(int i=1;i<=D;i++) G[F+2*N+i][ed]=1;
        for(int i=1;i<=N;i++){
            int k1,k2,u;
            scanf("%d%d",&k1,&k2);
            for(int j=1;j<=k1;j++){
                scanf("%d",&u);
                G[u][F+i]=1;
            }
            for(int j=1;j<=k2;j++){
                scanf("%d",&u);
                G[F+N+i][2*N+F+u]=1;
            }
        }
        printf("%d
    ",Edmonds_Karp());
    
        return 0;
    }
     
  • 相关阅读:
    linux ss 网络状态工具
    如何安装最新版本的memcached
    如何通过XShell传输文件
    mysql主从复制原理
    聊聊IO多路复用之select、poll、epoll详解
    聊聊 Linux 中的五种 IO 模型
    pytorch中使用cuda扩展
    pytorch中调用C进行扩展
    双线性插值
    python中的装饰器
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5618860.html
Copyright © 2011-2022 走看看