The maximum flow template for network flow

Source: Internet
Author: User
Tags bool

The

Dinic algorithm, the complexity of On*n*m (n is the number of points, M is the edge count), because the average number of points is less than the amount of edges, so more than Ek (om*m*n) a bit better.

int HEAD[MAXN], CNT;
int n, M, Tim[maxn], S, t;
BOOL VIS[MAXN];
int D[MAXN], CUR[MAXN];

struct Edge {int u, V, cap, flow;} e[maxn*600];

Vector<int> G[MAXN];
    void Init () {memset (head,-1, sizeof (head));
    memset (g, 0, sizeof (g));
    memset (Tim, 0, sizeof (TIM));
CNT = 0;
    } void Add (int u, int v, int cap, int f) {e[cnt].u = u;
    E[cnt].cap = cap;
    E[cnt].flow = f;
E[CNT].V = v;
    } void Addedge (int u, int v, int cap) {Add (U, V, cap, 0);   G[u].push_back (cnt++);
    G[I][J] = The number of edges that are saved.
    Add (V, u, 0, 0);
G[v].push_back (cnt++);
    } bool BFS () {memset (Vis, 0, sizeof (VIS));
    Queue<int> Q;
    Q.push (s);
    Vis[s] = 1;
    D[s] = 0;
        while (!q.empty ()) {int v = q.front (); Q.pop ();
            for (int i = 0; i < g[v].size (); i++) {//Direct reference, if the assignment is not replaced by this.   Edge &te = e[g[v][i]];
            For convenience, TE here is equivalent to E[g[v][i]], so write//For the convenience of the back to write. if (!VIS[TE.V] && te.cap > tE.flow) {//Only consider the arc vis[te.v of the residual network] = 1;  D[TE.V] = D[v] + 1;
            Layered Q.push (TE.V);
}}} return vis[t];   } int dfs (int x, int a) {if (x = = T | | a = = 0) return A;
    A.
    int flow = 0, F;
        for (int &i = cur[x]; i < g[x].size (); i++) {//from the last considered arc Edge &te = e[g[x][i]];
            if (d[x] + 1 = = d[te.v] && (f = dfs (te.v, Min (A, te.cap-te.flow))) > 0) {te.flow + = f;
            E[g[x][i]^1].flow-= f;//You know the relationship between the reverse arc and the original arc in the linked table because of the addition of the edge. It can be represented by the ^1.
            Flow + + F;
            A-= f;
        if (a = = 0) break;
}} return flow;
    } int dinic () {int flow = 0;
        while (BFS ()) {memset (cur, 0, sizeof (cur));   Flow + = DFS (s, INF); The INF for a cannot be set too large. Generally set to 1e9.
    Otherwise it will be t.
} return 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.