• 模板


    注意网络流的边的上限是两倍,因为要有反向边。 

    注意最后传入的n在最新版本中一般传入t(假如t是最后一个节点的话,或者不传入)就可以了。

    使用前要先init(),再添加addedge()。

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    
    /* dinic begin */
    
    const int MAXN=10100;
    const int MAXM=200100;
    //注意网络流要预留反向边
    const int INF=0x3f3f3f3f;
    struct Edge{
        int to,next,cap,flow;
    }edge[MAXM];
    
    int tol;
    int head[MAXN];
    
    void init(){
        tol=2;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int w,int rw=0){
        edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
        edge[tol].next=head[u];head[u]=tol++;
        edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;
        edge[tol].next=head[v];head[v]=tol++;
    }
    
    int Q[MAXN];
    int dep[MAXN],cur[MAXN],sta[MAXN];
    bool bfs(int s,int t,int n){
        int front=0,tail=0;
        memset(dep,-1,sizeof(dep[0])*(n+1));
        dep[s]=0;
        Q[tail++]=s;
        while(front<tail){
            int u=Q[front++];
            for(int i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].to;
                if(edge[i].cap>edge[i].flow&&dep[v]==-1){
                    dep[v]=dep[u]+1;
                    if(v==t)
                        return true;
                    Q[tail++]=v;
                }
            }
        }
        return false;
    }
    
    int dinic(int s,int t,int n=-1){
        int maxflow=0;
        if(n==-1)
            n=t;
        n++;//假如把t作为编号最后的点的话传入t就可以了
        while(bfs(s,t,n)){
            for(int i=0;i<n;i++)cur[i]=head[i];
            int u=s,tail=0;
            while(cur[s]!=-1){
                if(u==t){
                    int tp=INF;
                    for(int i=tail-1;i>=0;i--){
                        tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
    
                    }
                    maxflow+=tp;
                    for(int i=tail-1;i>=0;i--){
                        edge[sta[i]].flow+=tp;
                        edge[sta[i]^1].flow-=tp;
                        if(edge[sta[i]].cap-edge[sta[i]].flow==0)
                            tail=i;
                    }
                    u=edge[sta[tail]^1].to;
    
                }
                else if(cur[u]!=-1&&edge[cur[u]].cap>edge[cur[u]].flow
                        &&dep[u]+1==dep[edge[cur[u]].to]){
                    sta[tail++]=cur[u];
                    u=edge[cur[u]].to;
                }
                else{
                    while(u!=s&&cur[u]==-1){
                        u=edge[sta[--tail]^1].to;
                    }
                    cur[u]=edge[cur[u]].next;
                }
            }
        }
        return maxflow;
    }
    
    /* 备注:
    1.检查MAXN与MAXM,注意预留反向边和额外边的位置
    2.每次建图的第一次addedge()前必须先init()
    3.不传入第三参数的dinic(s,t)需保证t是最后的结点
     */
    
    /* dinic end */
  • 相关阅读:
    查找算法(I) 顺序查找 二分查找 索引查找
    快速排序 Quick Sort
    Activity生命周期
    Android中资源文件的使用
    排序算法
    插入排序(I)Insert Sort
    Java eclipse调试技巧什么的。。
    HTTP协议解析
    python技巧26[str+unicode+codecs]
    python类库26[PySide之helloworld]
  • 原文地址:https://www.cnblogs.com/Yinku/p/10638451.html
Copyright © 2020-2023  润新知