• POJ 3281 Dining(网络流)


                                                                        Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 16967   Accepted: 7535

    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

    【分析】典型的网络流题,关键是建图。对于每头牛,既要满足得到食物,又要满足得到饮料,所以将源点与所有食物建边,所有饮料与汇点建边,由于每头牛只能占领一种食物和饮料,
    所以考虑将牛拆点,i->i+n,这样避免了一头牛占领多种食物和饮料,然后食物到牛,牛到饮料,容量全为1.
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 1e3+10;
    const int M = 24005;
    int n,m,k,f,d;
    struct Edge{
        int from,to,cap,flow;
        Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
    };
    struct Dinic{
        int s,t;
        vector<Edge>edges;
        vector<int> G[N];
        bool vis[N];
        int d[N];
        int cur[N];
        void init(){
           for (int i=0;i<=n+1;i++)
               G[i].clear();
           edges.clear();
        }
        void AddEdge(int from,int to,int cap){
            edges.push_back(Edge(from,to,cap,0));
            edges.push_back(Edge(to,from,0,0));
            int mm=edges.size();
            G[from].push_back(mm-2);
            G[to].push_back(mm-1);
        }
        bool BFS(){
            memset(vis,0,sizeof(vis));
            queue<int>q;
            q.push(s);
            d[s]=0;
            vis[s]=1;
            while (!q.empty()){
                int x = q.front();q.pop();
                for (int i = 0;i<G[x].size();i++){
                    Edge &e = edges[G[x][i]];
                    if (!vis[e.to] && e.cap > e.flow){
                        vis[e.to]=1;
                        d[e.to] = d[x]+1;
                        q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
    
        int DFS(int x,int a){
            if (x==t || a==0)
                return a;
            int flow = 0,f;
            for(int &i=cur[x];i<G[x].size();i++){
                Edge &e = edges[G[x][i]];
                if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                    e.flow+=f;
                    edges[G[x][i]^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if (a==0)
                        break;
                }
            }
            return flow;
        }
    
        int Maxflow(int s,int t){
            this->s=s;
            this->t=t;
            int flow = 0;
            while (BFS()){
                memset(cur,0,sizeof(cur));
                flow+=DFS(s,inf);
            }
            return flow;
        }
    }dc;
    int main(){
        scanf("%d%d%d",&n,&f,&d);
        int F,D,u;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&F,&D);
            while(F--){
                scanf("%d",&u);
                dc.AddEdge(2*n+u,i,1);
            }
            while(D--){
                scanf("%d",&u);
                dc.AddEdge(i+n,2*n+f+u,1);
            }
            dc.AddEdge(i,i+n,1);
        }
        for(int i=1;i<=f;i++)dc.AddEdge(0,2*n+i,1);
        for(int i=1;i<=d;i++)dc.AddEdge(2*n+f+i,1000,1);
        printf("%d
    ",dc.Maxflow(0,1000));
        return 0;
    }
  • 相关阅读:
    SQL中文转拼音
    cocos2D 虚拟摇杆Joystick功能实现
    cocos2d 粒子效果以及Particle Designer粒子工具的学习
    android 模拟器出错,emulator: ERROR: unknown virtual device name
    [转][越狱破解] 苹果itouch 4 iOS5.0.1完美越狱教程+资源下载
    objectivec 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    [转]cocos2dx添加广告条(IOS and Android)
    cocos2d1.0.1x0.10.0版本 设置横屏与竖屏的方法
    【转】总结阐述Cocos2dX与Cocos2diphone区别;
    Objectivec 中CGGeometry几何类常用方法简单整理
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6434906.html
Copyright © 2020-2023  润新知