zoukankan      html  css  js  c++  java
  • 网络流板子(Dicnic)

    int cnt,s,t,n,m;
    int head[maxn],Next[maxn*20],to[maxn*20];
    int flow[maxn*20],dep[maxn],cur[maxn],mp[405][405];
    int dir[8][2]={1,2,1,-2,-1,2,-1,-2,-2,-1,-2,1,2,1,2,-1};
    inline void add(int u,int v,int w){
        Next[cnt]=head[u];
        head[u]=cnt;
        to[cnt]=v;
        flow[cnt++]=w;
    
        Next[cnt]=head[v];
        head[v]=cnt;
        to[cnt]=u;
        flow[cnt++]=0;
    }
    int dfs(int u,int cost){
        if(u==t) return cost;
        for(int &i=cur[u];i!=-1;i=Next[i]){
            if(dep[to[i]]==dep[u]+1&&flow[i]>0){
               int dis=dfs(to[i],min(flow[i],cost));
                if(dis>0){
                    flow[i]-=dis;
                    flow[i^1]+=dis;
                    return dis;
                }
            }
        }
        return 0;
    }
    int bfs(){
        queue<int> q;
        q.push(s);
        memset(dep,0,sizeof(dep));
        dep[s]=1;
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=Next[i]){
                if(flow[i]>0&&dep[to[i]]==0){
                    dep[to[i]]=dep[u]+1;
                    q.push(to[i]);
                }
            }
        }
        if(dep[t]>0) return 1;
        return 0;
    }
    int dicnic(){
        int ans=0;
        while(bfs()){
            int cost;
            for(int i=0;i<=t+2;i++){
                cur[i]=head[i];
            }
            while(cost=dfs(s,inf))
                ans+=cost;
        }
        return ans;
    }
    void init(){
        memset(head,-1,sizeof(head));
        cnt=0;
    }
    View Code
  • 相关阅读:
    Ubuntu16.04更新记
    「BZOJ2153」设计铁路
    [UVA-11995]I Can Guess the Data Structure!
    [UVA-11100] The Trip
    [UVA-11039]Children's Game
    [BZOJ1008][HNOI2008]越狱
    NOIP2018退役祭
    修马路
    [NOIP2005]过河
    [POJ1958][Strange Tower of Hanoi]
  • 原文地址:https://www.cnblogs.com/MengX/p/10574838.html
Copyright © 2011-2022 走看看