dinic+ Current ARC optimization

Source: Internet
Author: User

The dinic algorithm uses the BFS to construct the hierarchical graph and then augment it with the blocking flow.

What is a hierarchy chart? Assuming that in the residual network, the distance from the starting point to the node U is dist (u), then this dist (U) is considered as the level of the node U, leaving only the arcs from each point to the next level, and the resulting graph is the level graph. The blocking stream is the maximum flow when the reverse arc is not considered. The maximum number of N-1 blocking flows is calculated (because the t increases by at least 1 at the level of the block relative to s) each time the blocking stream is augmented, and the time to block the stream is less than O (MN), so the total time complexity is O (n^2m), but actually faster than this.

What does the current arc optimization mean? Note that using cur[x] in DFS means that the current should start access from the edge of the X's number cur[x], which means that the edges from 0 to cur[x]-1 are no longer accessible, equivalent to being deleted and reaching full stream. DFS (X,a) represents the current x node, which has a maximum flow of flow A, to the end point T. The current ARC optimization key point in DFS is in the IF (a==0) break; that is, for node x, if the front of the X connection is already able to send so much traffic to the end, there is no need to access some of the back arcs. The current not full arc and the subsequent unreachable arc wait until the next visit to the node X is augmented.

struct edge{int from,to,cap,flow;};            struct dinic{int n,m,s,t;     Node count, number of edges (including reverse arc), source point number and meeting point number vector<edge> edges; Edge table.    Edge[e] and edge[e^1] each other for reverse arc vector<int> G[MAXN];         adjacency table, G[i][j] indicates the sequence number of the node I and J Edge in the e array bool VIS[MAXN];            BFS using int D[MAXN];          Distance from starting point to I int CUR[MAXN];
        Current arc subscript void Clear_all (int n) {for (int i=0;i<n;i++) g[i].clear ();
    Edges.clear ();
        } void Clear_flow () {int len=edges.size ();
    for (int i=0;i<len;i++) edges[i].flow=0;
        } void Add_edge (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 () {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 ();
            int len=g[x].size ();
                for (int i=0;i<len;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,len=g[x].size ();
            for (int& i=cur[x];i<len;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;
        } vector<int> Mincut () {//call This after maxflow vector<int> ans;
        int len=edges.size ();
            for (int i=0;i<len;i++) {edge& e=edges[i];
        if (vis[e.from]&&!vis[e.to]&&e.cap>0) ans.push_back (i);
    } return ans;
        } void reduce () {int len=edges.size ();
    for (int i=0;i<len;i++) Edges[i].cap-=edges[i].flow; }
};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.