Graph theory algorithm-network maximum flow "Ek;dinic"

Source: Internet
Author: User

graph theory algorithm-Network maximum flow template "Ek;dinic" ek Templates

Increase traffic by finding the minimum residue in the augmented residual network every time

const int Inf=1e9;int n,m,s,t;struct node{int v,cap;}; vector<node> map[100010];int flow[10010][10010];int a[100010];int pre[100010];int EK () {int maxf;//record maximum flow Queu    E<int> Q;        while (1) {memset (a,0,sizeof (a));        A[s]=inf;                Q.push (s);            while (!q.empty ()) {int U=q.front ();            Q.pop ();                for (int j=0;j<map[u].size (); j + +) {int v=map[u][j].v;                int cap=map[u][j].cap;                    if (!a[v]&&cap>flow[u][v]) {pre[v]=u;                    Q.push (v);                A[v]=min (A[u],cap-flow[u][v]);                }}} if (a[t]==0) break;            for (int u=t;u!=s;u=pre[u]) {flow[pre[u]][u]+=a[t];        flow[u][Pre[u]]-=a[t];    } Maxf+=a[t]; } return MAXF;}    int main () {cin>>n>>m>>s>>t; for (int i=1;i<=m;i++) {int u,v,dis;        cin>>u>>v>>dis;        Map[u].push_back (node) {V,dis});        Map[v].push_back (node) {u,0}),//** highlight * * Reverse edge must remember to build} int Ans=ek ();    cout<<ans; return 0;}
Dinic templates

Construct hierarchy + Block flow augmentation

const int Inf=1e9;int n,m;int s,t;int tot=1;struct node{int v,f,nxt;}    E[1000010];int head[100010];int lev[100010];//Record level void Add (int u,int v,int cap) {e[++tot].v=v;    E[tot].nxt=head[u];    E[tot].f=cap; Head[u]=tot;}    BOOL BFs () {queue<int> q;    memset (Lev,-1,sizeof (Lev));    Q.push (s);        lev[s]=0;        while (!q.empty ()) {int U=q.front ();        Q.pop ();            for (int i=head[u];i;i=e[i].nxt) {int v=e[i].v;                if (LEV[V]==-1&AMP;&AMP;E[I].F) {lev[v]=lev[u]+1;                if (v==t) return true;            Q.push (v); }}} return false;}        int dfs (int u,int cap) {if (u==t) return cap;    int flow=cap;        for (int i=head[u];i;i=e[i].nxt) {int v=e[i].v;            if (lev[v]==lev[u]+1&&flow&&e[i].f>0) {int F=dfs (v,min (FLOW,E[I].F));            Flow-=f;            E[i].f-=f;        E[i^1].f+=f; }} RETurn Cap-flow;}       int dinic () {int maxf=0; while (BFS ())//If S-T can be reached on the continuous structure of the hierarchy Maxf+=dfs (s,inf);//s-t, augmented with a blocking stream return MAXF;}    int main () {cin>>n>>m>>s>>t;        for (int i=1;i<=m;i++) {int u,v,w;        cin>>u>>v>>w;        Add (U,V,W);    Add (v,u,0);//** Highlight * * Reverse side must remember to build} dinic ();    cout<<maxf; return 0;}

Graph theory algorithm-network maximum flow "Ek;dinic"

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.