A more efficient cost-flow algorithm--zkw cost stream _ algorithm

Source: Internet
Author: User

Orz Creator zkw%%%
Send the zkw god Benniu's blog in situ: Portal

The cost stream is based on the maximum flow of the network, the largest stream in a graph has and has only one, but the maximum number of stream bars is often more than one, and for us it may be necessary to find the smallest (or largest) path in these maximum flows (greedy strategy), which is the minimum (maximum) cost maximum flow
--that's the definition.

We are seeking the cost of the flow of the algorithm is also very much, the most popular is the EDMOND-KARP after the revised cost flow algorithm, this I will not say more, online tutorials are also very many

Let's analyze the advantages and disadvantages of EK: The essence of the EK algorithm is to replace the original BFS with the SPFA to calculate the minimum cost, is a wide range along the shortest path of the algorithm, so more efficient and simple to obtain the answer
A small drawback is that the EK is a single channel, so that the speed will be relatively slow a bit (but for the cost stream today's data range will not be too large, EK problem is not)

Then ZKW has made some improvements to the problem, such as the multipath augmentation when Dfs, the KM algorithm saves SPFA time, etc.

ZKW's original code is detailed in the link above.

However, the KM algorithm does not do it. In fact, SPFA can still do, we use SPFA to maintain the distance of each point marking, and then use the ZKW in the DFS of the idea of more than a wide range, so that the time efficiency can be much faster (and this seems to be able to run a negative weight map, but the original zkw can not)

The specific code implementation is as follows (Luogu3381 "template" minimum cost maximum flow):

#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include < iostream> #include <ctime> #include <map> #include <queue> #include <cstdlib> #include <
string> #include <climits> #include <set> #include <vector> using namespace std;
BOOL Vis[200001];int dist[200001];
Explain the meaning of each array: Vis two uses: SPFA in the access tag, the access tag, dist is the distance of each point of the label int n,m,s,t,ans=0;
S is the starting point, T is the endpoint, ans is the cost answer int nedge=-1,p[200001],c[200001],cc[200001],nex[200001],head[200001];
Here is the side table, explain the meaning of each array: P[i] Indicates a point of departure with the number I of the side of the corresponding points, C for the number of the side of the flow, CC is the number I of the side of the cost, NEX and head do not say it ... 
inline void Addedge (int x,int y,int z,int zz) {P[++nedge]=y;c[nedge]=z;cc[nedge]=zz;nex[nedge]=head[x];head[x]=nedge;
    }//Jianben (array analog side table upside down) inline bool SPFA (int s,int t) {memset (vis,0,sizeof vis);
for (int i=0;i<=n;i++) dist[i]=1e9;dist[t]=0;vis[t]=1;
First SPFA We maintain the distance marking the time to run backwards, so that you can maintain the shortest path to the end of the Deque<int>q;q.push_back (t); Using the SPFA SLF optimization (SLF can own Baidu or Google) while (!q.empty ()) {int Now=q.front (); Q.pop_front (); for (int k=head[now];k>-1;k=nex[k]) if (C[k^1]&&dist[p[k]]>dist[now]-cc[k]) {//First c[k^1] why,
Because we want to ensure that the flow, but SPFA is running backwards, so that we require c[k] corresponding to the opposite side is positive, so that the direction of the way is correct dist[p[k]]=dist[now]-cc[k];
                Because it is already upside down, we can clearly understand that when the edge of the Benquan is negative, so minus the right (negative positive) if (!vis[p[k]) {vis[p[k]]=1;
if (!q.empty () &&dist[p[k]]<dist[q.front ()]) Q.push_front (p[k); else Q.push_back (p[k));
    SLF optimization}} vis[now]=0;
Return dist[s]<1e9;
    To determine whether the end point is connected} inline int dfs (int x,int low) {//This is where it's been made. if (x==t) {Vis[t]=1;return low;}
int used=0,a;vis[x]=1; Is this this like dinic? for (int k=head[x];k>-1;k=nex[k]) if (!vis[p[k]]&&c[k]&&dist[x]-cc[k]==dist[p[k
        ] {//This condition means that the edge can be A=dfs (P[k],min (c[k],low-used));
if (a) ans+=a*cc[k],c[k]-=a,c[k^1]+=a,used+=a;
    Cumulative answer, Gareu and other operations are in this if (used==low) break;
return used; } inline int Costflow () {int flow=0;
        while (SPFA (s,t)) {//To determine whether the beginning of the end is connected, disconnected description full flow, finished the exit vis[t]=1;
            while (Vis[t]) {memset (vis,0,sizeof vis);
Flow+=dfs (S,1E9); Keep it up until it's too late (this can also save time OH)} return flow;//here is the maximum flow, the cost of the answer in ans} int main () {memset (nex,-1,sizeof NE
    x); memset (head,-1,sizeof head);
    scanf ("%d%d%d%d", &n,&m,&s,&t);
        for (int i=1;i<=m;i++) {int x,y,z,zz;scanf ("%d%d%d%d", &x,&y,&z,&zz);
    Addedge (X,Y,Z,ZZ); Addedge (Y,X,0,-ZZ);
    printf ("%d", Costflow ());p rintf ("%d", ans);
return 0; }

After reading the code the feeling is: Wow and dinic like AH

Finally, the advantages and disadvantages of zkw cost flow, the original advantage is to save a lot of running SPFA time, there are many ways to increase the large advantage, this feature can be in many ways when the cost of the same time to come in handy, further reducing the time spent on heavy marking

and SPFA version of the multiplex even if more than run SPFA, the advantages of more than a wide range still exist, so the time is quite excellent, coupled with SLF optimization after the reduction of the constant is very efficient

And sometimes zkw disadvantage is also some, for the flow is not small, cost, augmented road is also longer network, ZKW embodiment of the advantage is not big, and perhaps even slower than ordinary ek ... So for different network zkw version and common EK version each have advantages, we can choose

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.