• Dining


    #include<algorithm>  
    #include<iostream>  
    #include<cstring>  
    #include<string>  
    #include<vector>  
    #include<cstdio>  
    #include<queue>  
    #include<cstdio>   
    #define N 10005  
    #define INF 0x3f3f3f3f  
    #define Del(a,b) memset(a,b,sizeof(a))   
    using namespace std;    
       
    struct Edge{    
        int from,to,cap,flow;    
    };   
       
    struct Dinic{    
        int n,m,s,t;    
        vector<Edge> edges;    
        vector<int> G[N];    
        bool vis[N];    
        int d[N],cur[N];    
         void init(int n){    
            this->n=n;    
            for(int i=0;i<=n;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});    
            m=edges.size();    
            G[from].push_back(m-2);    
            G[to].push_back(m-1);    
        }    
        bool BFS(){    
            Del(vis,0);    
            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 max_flow(int s,int t){    
            this->s=s;this->t=t;    
            int flow=0;    
            while(BFS()){    
                Del(cur,0);    
                flow+=DFS(s,INF);    
            }    
            return flow;    
        }    
    };    
       
    Dinic solve;  
       
    int main(){    
        int n,m,q;    
        while(~scanf("%d%d%d",&n,&m,&q)){ 
            solve.init(505); 
            int s,t;s=1;int x,y;t=402;
            for(int i=2;i<=m+1;++i)
                solve.AddEdge(1,i,1);
            for(int i=0;i<n;i++)
                for(int j=1;j<=m;++j){        
                    scanf("%d",&x); 
                    if(x)
                    solve.AddEdge(j+1,i+101,1);    
                }    
            for(int i=1;i<=n;++i)
                solve.AddEdge(i+101,i+201,1);
            for(int i=0;i<n;i++)
                for(int j=1;j<=q;++j){        
                    scanf("%d",&x);if(x)
                    solve.AddEdge(i+201,j+301,1);    
                }
            for(int i=1;i<=q;++i)
                solve.AddEdge(301+i,402,1);
            printf("%d
    ",solve.max_flow(s,t));   
        }    
        return 0;    
    }   
    
  • 相关阅读:
    新人入住博客,互相交流,互相进步
    DHCP技术
    一些额外技术
    OSPF技术
    一些常用的命令
    RSTP技术及原理
    BFD原理
    VRRP技术
    Super VLAN技术
    哈希表(HashMap)
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/8093026.html
Copyright © 2020-2023  润新知