Network flow (maximum flow, minimum cost maximum flow, network flow with upper and lower bounds) __ACM-ICPC

Source: Internet
Author: User
Algorithm for maximum flow:
Ford-fulkerson algorithm to maximize the flow of the process is to continue to find a source to the sink path, then we construct the residual network, then find the new path on the residual network, make the total flow increase, then form the new residual network, then find the new path .... Until a path from source to sink is not found on a residual network   , the maximum flow even out. The process of finding new traffic and constructing a new residual network each time it's called the "augmented path" of the search for traffic, also called "augmentation." Now assume that each edge is an integer. This algorithm can increase the flow by at least 1 each time because the flow of the entire network does not exceed the capacity of all the edges in the graph and C, so the algorithm will end now To see the complexity of the algorithm to find the augmented path can be used DFS, the complexity of the number of edge m+ vertex n Dfs run up to C so the time complexity of c* (m+n) =c* n^ http://acm.hdu.edu.cn/showproblem.php?pid=1532
#include <bits/stdc++.h> using namespace std;
const int n=1e3+107;
const int inf=0x3f3f3f3f;
struct node{int to;//endpoint int cap;//capacity int rev;//reverse Edge};
Vector<node> V[n];
BOOL Used[n];
    void Add (int from,int to,int cap) {V[from].push_back ((node) {to,cap, v[to].size ()});
V[to].push_back (node) {from,0, v[from].size ()-1});
    int dfs (int s,int t,int f) {if (s==t) return F;
    Used[s]=true;
        for (int i=0;i<v[s].size (); i++) {node &tmp=v[s][i];
            if (used[tmp.to]==false&&tmp.cap>0) {int D=dfs (tmp.to,t,min (F,tmp.cap));
                if (d>0) {//Tmp.cap-=d;
                V[s][i].cap-=d;
                V[tmp.to][tmp.rev].cap+=d;
            return D;
}} return 0;
    int max_flow (int s,int t) {int flow=0;
        while (1) {memset (used,false,sizeof (used));
        int F=dfs (s,t,inf);
        if (f==0) return flow; FLow+=f;
    int main () {int n,m,i,j,k,t;
        while (~SCANF ("%d%d", &n,&m)) {memset (v,0,sizeof (v));
            for (i=0;i<n;i++) {int x,y,z;
            scanf ("%d%d%d", &x,&y,&z);
        Add (x,y,z);
    printf ("%d\n", Max_flow (1,m));
return 0; }
Dinic algorithm   EDMONDS-KARP: Multiple calls to BFS from S to T can be attempted to reduce the number of calls.  is the use of a less costly and efficient augmented approach.  consider: In an augmented process, look for multiple augmented paths. The implementation of dfs  Dinic algorithm has the following steps: 1: Initialize capacity network and network Flow 2: constructs the residual network and the hierarchical network, if the meeting point is not in the hierarchical network, the algorithm ends the output maximum Flow 3: In the hierarchical network with a DFS for augmentation, DFS execution completed, This phase of the augmentation will be completed. 4: Go to step 2
#include <bits/stdc++.h> using namespace std;
const int n=205;
const int m=100010;
const int inf=0x3f3f3f3f;
int n,m,si,ei,ci;
int level[n]; struct node{int c,f;}
Mp[n][n];
    BOOL Dinic_bfs () {queue<int> q;
    memset (level,0,sizeof level);
    Q.push (1);
    Level[1]=1;
        while (!q.empty ()) {int U=q.front ();
        Q.pop (); for (int v=1;v<=m;v++) if (!LEVEL[V]&AMP;&AMP;MP[U][V].C&GT;MP[U][V].F) {level[v]=level[u]+1
            ;
        Q.push (v);
} return level[m]!=0;
    int Dinic_dfs (int u,int cp) {int TMP=CP;
    if (u==m) return CP;
            for (int v=1;v<=m&&tmp;v++) if (Level[u]+1==level[v]) {if (MP[U][V].C&GT;MP[U][V].F) {
            int T=dinic_dfs (V,min (TMP,MP[U][V].C-MP[U][V].F));
            mp[u][v].f+=t;
            mp[v][u].f-=t;
        tmp-=t;
} return cp-tmp;
    int dinic () {int sum=0,tf=0; while (DINIC_BFS ()) {WHILe (Tf=dinic_dfs (1,inf)) {SUM+=TF;
return sum;
        int main () {while (~scanf ("%d%d", &n,&m)) {memset (mp,0,sizeof MP);
            while (n--) {scanf ("%d%d%d", &si,&ei,&ci);
        Mp[si][ei].c+=ci;
    printf ("%d\n", Dinic ()); return 0;
Minimum cost maximum flow
1. Find the minimum cost from the point of origin to the receiving point U (s,t) 2. Assign the maximum possible flow to the path U (s,t): F and allow the capacity of all sides on the path to be reduced by f accordingly. At this time, for the saturation side of the path, the unit flow cost is changed to + Infinity 3. As the reverse side (j,i) of all the sides (I,J) on the path U (s,t), make 4. Repeat the above steps in the new network that constitutes this 1,2,3, The total flow from the point to the point of receipt is equal to f (or no minimum cost path from S to T can be found).
Repeatedly using the SPFA algorithm to do the source to the sink of the shortest path to expand, edge weights for the edge of the unit costs.   The unit cost on the reverse side is negative.    Until it is not augmented, the minimum cost maximum flow is found.   Reason: Each time augmentation, each additional flow, the increase in the cost is the smallest. Because there is a negative right edge (when the flow is canceled), so can not use the Dijkstra algorithm to find the shortest path.
http://poj.org/problem?id=2135
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h> #include
<stdlib.h> #include <cstring> using namespace std;
const int n=1050;
const int m=40050;
const int inf=0x3f3f3f3f;
    struct node{int from,to,flow,cap,cost;
    Node () {} node (int a,int b,int c,int d,int e) {from=a;to=b;flow=c;cap=d;cost=e;
}

};
Vector<int> G[n];
Vector<node> MP;
    void Add (int a,int b,int c,int d) {Mp.push_back (node (a,b,0,c,d));
    Mp.push_back (Node (b,a,0,0,-d));
    int total=mp.size ();
    G[a].push_back (TOTAL-2);
G[b].push_back (TOTAL-1);
int dis[n],p[n],a[n];
BOOL Used[n];
int m,n,flow,cost;
    BOOL SPFA (int s,int t) {memset (dis,inf,sizeof dis);
    memset (used,0,sizeof used); Dis[s]=0;used[s]=1; p[s]=0;
    A[s]=inf;
    Queue<int> Q;
    Q.push (s);
        while (!q.empty ()) {int U=q.front (); Q.pop ();
        used[u]=0; for (int i=0;i<g[u].size (); i++) {node Tmp=mp[g[u][i]]; 
                if (tmp.cap>tmp.flow&&dis[tmp.to]>dis[u]+tmp.cost) {dis[tmp.to]=dis[u]+tmp.cost;
                P[tmp.to]=g[u][i];
                A[tmp.to]=min (A[u],tmp.cap-tmp.flow);
            if (!used[tmp.to]) Q.push (tmp.to), used[tmp.to]=1;
    }} if (Dis[t]==inf) return 0;
    FLOW+=A[T];
    COST+=DIS[T]*A[T];
    int u=t;
        while (u!=s) {mp[p[u]].flow+=a[t];
        MP[P[U]^1].FLOW-=A[T];
    U=mp[p[u]].from;
return 1;
    int mincost (int s,int t) {flow=cost=0;
    while (SPFA (s,t));
return cost;
        int main () {while (~scanf ("%d%d", &n,&m)) {memset (g,0,sizeof g);
        Mp.clear ();
        int s=0,t=n+1,u,v,c;
            while (m--) {scanf ("%d%d%d", &u,&v,&c);
            Add (U,V,1,C);
        Add (V,U,1,C);
        Add (s,1,2,0);
        Add (n,t,2,0);
    printf ("%d\n", Mincost (s,t)); } RETurn 0;
 }

Network flow with upper and lower bounds
1, no Yuanhui has the upper and lower bounds of the maximum flow problem thinking: O (-1). Modeling Model: Previously written maximum stream default lower bound is 0, and here the lower bound is not 0, so we have to reconstruct the lower bound of each edge to 0, this is to facilitate processing. For each tube has an upper bound capacity up and a lower bound capacity of low, we let this tube's capacity lower bound to 0, upper bound for Up-low. But this is done the flow is not conserved, in order to meet the flow of conservation again, that is, each node "inflow = Flow", we add a super source Point St and a super Terminal SD. We open an array du[] to record the flow of each node.
Du[i]=in[i] (I node all into the stream boundary of the sum)-out[i] (I node all out of the flow boundary of the sum).
When Du[i] is greater than 0, the St to I with a flow of du[i] edge.
When Du[i] is less than 0, I to SD with a flow of-du[i] edge.
Finally, a maximum flow is obtained for the (ST,SD), and there is a feasible solution when all the additional edges are full stream (that is, maxflow== all the du[]>0).
Topic Link: sgu194 reactor cooling
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <vector>


#include <queue> #include <algorithm> using namespace std;
const int MAXN=1000+10;

const int INF=0X7FFFFFFF;
struct Edge {int from,to,cap,flow,up,low;};
vector<edge>edges;
vector<int>g[maxn];
BOOL VIS[MAXN];
int D[MAXN];
int CUR[MAXN];
int OUT[MAXN],IN[MAXN];

int s,t;
    To find the hierarchical network 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 ();
            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];
    }//Add-edge void Addedge (int from,int to,int low,int up) {int m;
    Edge R;
 R.from=from;   R.to=to;
    R.cap=up-low;
    r.flow=0;
    R.low=low;
    R.up=up;
    Edges.push_back (R);
    Edge D;
    D.from=to;
    D.to=from;
    D.cap=0;
    d.flow=0;
    R.low=low;r.up=up;
    Edges.push_back (d);
    M=edges.size ();
    G[from].push_back (m-2);
G[to].push_back (m-1); //per phase DFS augmented 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;
///Multiple stages, establish hierarchical network multiple times.
    int maxflow (int ss,int tt) {int flow=0;
        while (BFS ()) {memset (cur,0,sizeof (cur));
    Flow+=dfs (Ss,inf);
} return flow;
    int main () {int n,m,i,j;
        while (~SCANF ("%d%d", &n,&m)) {edges.clear (); for (iNT I=0; i<maxn;
        i++) g[i].clear ();
        memset (In,0,sizeof (in));
        Memset (out) (out,0,sizeof);
        s=0;

        t=n+1;
            For (I=1 i<=m; i++) {int u,v,low,up;
            scanf ("%d%d%d%d", &u,&v,&low,&up);
            Addedge (U,v,low,up);
            In[v]=in[v]+low;
        Out[u]=out[u]+low;
            For (I=1 i<=n; i++) {Addedge (i,t,0,out[i));
        Addedge (S,i,0,in[i]);

        } maxflow (S,t);
        int flag=1;
                For (i=0 i<edges.size (); i++) if (edges[i].from==s) if (EDGES[I].FLOW&LT;EDGES[I].CAP)
                    {flag=0;
                Break
        } if (!flag) printf ("no\n");
            else {printf ("yes\n");
        For (i=0 i<2*m; i++) if (i%2==0) printf ("%d\n", Edges[i].flow+edges[i].low);
} return 0; }



2, there are Yuanhui with the upper and lower bounds of the maximum flow problem thinking: Add a source Point St, meeting point Sd,st to day I even one of the upper bound of Di lower bound to 0, each goddess to the meeting point of the lower bound to the GI upper bound for the OO side, for each day, the day to the first girl even a [Li,ri] side. Build model: Source point S, Endpoint d. Super Source Point SS, Super endpoint DD. First of all, to determine whether there is a feasible flow that satisfies all the upper and lower bounds, the method can be transformed into a feasible flow problem with no Yuanhui and upper and lower bounds. How to convert it.
Add a side from D to s that has no lower bound capacity, then the original image becomes a Yuanhui circular flow diagram. Next thing like that, super Source point SS-Lian I (du[i]>0), I even Super meeting point (DU[I]&LT;0),
to (SS,DD) a maximum flow, when Maxflow equals all (du[]>0) of the sum, there is a feasible flow, otherwise no.
When there is a feasible flow, remove the super source point SS and Super Endpoint DD, and then the (s,d) for a maximum flow, at this point the maxflow is the problem of the target solution. Why, then? Because the first maxflow () is to obtain all the traffic that satisfies the lower bound, on the residual network (S,D) There are also many free streams (no edges connected to the super source and super sinks) that are not full, all the resulting maxflow= (the first stream full of lower bound flows + the second flow of free flow).
Title: http://poj.org/problem?id=2396: http://blog.csdn.net/water_glass/article/details/6823741 #include < iostream>   #include <cstdio>   #include <string>   using  namespace std;   #define  maxM 50000   #define  maxN 500   # define inf 1<<30   struct node{       int u,v, f,next;  }edge[maxm];   int head[maxn],p,lev[maxn],cur[maxn];   int que[ maxm],tre[maxn],up[maxn][50],low[maxn][50];   void init1 (int n,int m) {       p=0,memset (head,-1,sizeof head), memset (Tre,0,sizeof (tre));        for (int i=0;i<=n;i++)  for (int j=0;j<=m;j++)             up[i][j]=inf,low[i][j]=0;  }   Bool bfs (int s,int &NBSP;T) {       int qin=0,qout=0,u,i,v;       memset (lev,0, sizeof (LEV));       lev[s]=1,que[qin++]=s;        while (Qout!=qin) {           u=que[qout++];            for (i=head[u];i!=-1;i=edge[i].next) {                if (edge[i].f>0 && lev[v=edge[i].v]= =0) {                    lev[v]=lev[u]+1,que[qin++]=v;                    if (v==t)  return 1;                }           }        }  

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.