"Algorithm" Network Max Stream EK

Source: Internet
Author: User

What does network streaming do? To give an example:

In a water city, there are many small towns, there are many bridges connected, each bridge because the production material different maximum load different, if the maximum load, the bridge will collapse, the people on the bridge on the GG, so we can not let this happen-that is: each side of the traffic can not exceed capacity, we set a starting point, An end, we want to from the beginning of goods to the end, there is only one chance but can take a number of roads at the same time to make full use of resources, and finally ask: what is the maximum shipment volume can be?

This is the network maximum flow problem, to find a point to a certain point of the maximum flow.

EK algorithm, the most simple network flow algorithm, constantly looking for augmented road, and then back and forth two times to reduce capacity, plus ans, easy to understand:

1#include <cmath>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intmaxn= $+Ten, inf=2000000000;8 intc[maxn][maxn],f[maxn][maxn],q[maxn][2],n,m,ans;//The c array is the record capacity, the F array is the record traffic, Q is the queue, because it is necessary to record the current point, but also to record the path, so open 2-D9 BOOLp[maxn],mark=true;//p represents whether each point is desirable (only once per point)TenInlinevoidReadint&x) One { Ax=0; -     CharC=GetChar (); -      while(c<'0'|| C>'9') c=GetChar (); the      while(c>='0'&&c<='9') -     { -X= (x<<3) + (x<<1) + (c^'0'); -C=GetChar (); +     } - } + intMain () A { at read (m); Read (n); -      for(RegisterintI=1; i<=m;++i)//input capacity, no connectivity capacity is 0 -     { -         intx, y, z - Read (x); Read ( y); read (z); -C[x][y]+=z;//It 's possible that there are multiple pipelines between the two points. in     } -      while(Mark) to     { +mark=false; -          for(RegisterintI=1; i<=n;++i) p[i]=true;//set each point to be desirable thep[1]=false;//Take the 1 away. *q[1][0]=1;//put in queue $q[1][1]=0;//1 The front path is 0Panax Notoginseng         intHead=0, tail=1;//the head and tail of the queue -          while(Head<tail)//go first, look for the path from the beginning to the end the         { +head++; A             intx=q[head][0];//Remove the first element of the team the              for(RegisterintI=1; i<=n;++i) +             { -                 if(p[i]&& (c[x][i]>f[x][i]| | F[i][x]>0))//judge if the point is preferable and the capacity is greater than the flow (because F is initialized to 0, and the value of the disconnected two-point C array is 0, so it is not counted) or if the current direction is reverse (no capacity in reverse) to determine whether there is a flow, if there is traffic, it represents the capacity $                 { $p[i]=false;//set to non-fetching -q[++tail][0]=i;//put in queue -q[tail][1]=head;//Record Path (note: The location of the current point in the queue is the head) the                     if(i==n)//If the current point is an endpoint, a path has been found -                     {Wuyimark=true; the                          Break;//just jump out of the loop -                     } Wu                 } -             } About             if(Mark) Break; $         } -         if(!mark) Break;//If you can't find the path, it's all done, just jump out of the loop. -         intMinn=inf,pos=tail;//Minn represents the bottleneck, POS is looking forward from the end point -          while(q[pos][1])//go the second time, looking for bottlenecks from behind//as long as the front of the current point is a bit A         { +             intx=q[q[pos][1]][0],y=q[pos][0];//x, Y is the two ends of a pipe, the first is the previous point, and Y is the back point the             if(C[x][y]>f[x][y]) minn=min (Minn,c[x][y]-f[x][y]);//if the capacity is larger than the flow, that can also flow, then update the bottleneck -             Else if(f[y][x]>0) Minn=min (minn,f[y][x]);//in another case, the maximum reverse flow is the forward flow $pos=q[pos][1];//make a search for the next point the         } thepos=tail; the          while(q[pos][1])//go three times, change traffic on the path the         { -             intx=q[q[pos][1]][0],y=q[pos][0]; in             if(C[x][y]>f[x][y]) F[x][y]+=minn;//forward, (forward) traffic plus bottlenecks the             ElseF[y][x]-=minn;//reverse, (forward) flow minus bottleneck thepos=q[pos][1]; About         } the     } the      for(RegisterintI=2; i<=n;++i) ans+=f[1][i];//The flow from the starting point to the end, all traffic to the start (or end) is the maximum flow theprintf"%d", ans); +     return 0; -}
Network Max Stream EK

"Algorithm" Network Max Stream EK

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.