EK algorithm template

Source: Internet
Author: User

#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespacestd;intdp[ -][ -],pre[ -];Const inttmin=999999999;intMaxflow;voidEK (intStartintEndintN) {     while(1) {Queue<int>P; Q.push (1);//source point is 1, incoming team       intminflow=tmin; memset (PRE,0,sizeof(pre));//Initializes an array of augmented paths, with the vertices in the title starting from 1         while(!q.empty ()) {//BFS find augmented Road            intu=Q.front ();            Q.pop ();  for(intI=1; i<=n;i++){                if(dp[u][i]>0&&!pre[i]) {//Pre[i] In addition to recording the current vertex of the father, also record the current vertex has not been visitedpre[i]=u;                Q.push (i); }            }        }        if(pre[end]==0)//the vertex of the father is empty, indicating that the augmented path can not be found, it is easy to understand it.              Break;  for(intI=end;i!=start;i=pre[i]) {//finding the minimum residual amount in the augmented pathminflow=min (dp[pre[i]][i],minflow); }         for(intI=end;i!=start;i=pre[i]) {//increased flow of positive and negative arcs in the wide roaddp[pre[i]][i]-=Minflow; Dp[i][pre[i]]+=Minflow; } Maxflow+=Minflow; }}intMain () {intCount=0; intn,m; intT; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m); Memset (DP,0,sizeof(DP)); memset (PRE,0,sizeof(pre)); Count++; intu,v,w;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&u,&v,&W); DP[U][V]+=W; } Maxflow=0; EK (1, N,n); printf ("Case %d:%d\n", Count,maxflow); }   return 0;}

EK algorithm template

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.