Network Flow Algorithm set EK dinic minimum cost maximum flow (Dijkstra implementation)

Source: Internet
Author: User

Finally, I have finished writing the template of the network stream. I have to paste a few files and use the forward star to save and save the trouble of writing the linked list, the code is definitely the most streamlined in the code you can find

// Ek
# Include <stdio. h>
# Include <iostream>
Using namespace STD;
# Include <memory. h>
# Define maxn300
# Define maxflow 2000000000
Int N, S, T, M, flow [maxn + 1] [maxn + 1], map [maxn + 1] [maxn + 1], pre [maxn + 1];
Void Init ()
{
Int I, A, B, C;
Memset (MAP, 0, sizeof (MAP ));
Memset (flow, 0, sizeof (flow ));
Memset (PRE, 0, sizeof (pre ));
Cin> N> S> T> m;
For (I = 1; I <= m; I ++)
{
Cin> A> B> C;
Map [a] [0] ++;
Map [a] [map [a] [0] = B;
Map [B] [0] ++;
Map [B] [map [B] [0] =;
Flow [a] [B] + = C; // for the existence of the duplicate edge, we recommend that you use + =
}
}
Int BFS ()
{
Int I, TT [maxn + 1], start, finish, chk [maxn + 1], K;
Memset (TT, 0, sizeof (TT ));
Memset (chk, 0, sizeof (chk ));
Start = 1; finish = 1; TT [1] = s;
While (start <= finish)
{
For (I = 1; I <= map [TT [start] [0]; I ++)
If ((! Chk [map [TT [start] [I]) & (flow [TT [start] [map [TT [start] [I])
{
K = map [TT [start] [I];
Finish ++;
TT [finish] = K;
Chk [k] = 1;
Pre [k] = TT [start];
If (k = T) return 1;
}
Start ++;
}
Return 0;

}
Int min (int A, int B)
{
If (A <B) return;
Else return B;
}
Int main ()
{
Int ans, K, V;
Init ();
Ans = 0;
While (BFS ())
{
K = T; V = maxflow;
While (K! = S)
{
V = min (v, flow [pre [k] [k]);
K = pre [k];
}
K = T; ans + = V;
While (K! = S)
{
Flow [pre [k] [k]-= V;
Flow [k] [pre [k] + = V;
K = pre [k];
}
}
Cout <ans <Endl;
}

 

 

 

// The maximum stream dinic writes more functions than EK to calculate the minimum number of edges from The Source Vertex To This vertex and adds a judgment condition.

# Include <stdio. h>
# Include <iostream>
Using namespace STD;
# Include <memory. h>
# Define maxn300
# Define maxflow 2000000000
Int N, S, T, M, flow [maxn + 1] [maxn + 1], map [maxn + 1] [maxn + 1], pre [maxn + 1], d [maxn + 1];
Void Init ()
{
Int I, A, B, C;
Memset (MAP, 0, sizeof (MAP ));
Memset (flow, 0, sizeof (flow ));
Memset (PRE, 0, sizeof (pre ));
For (I = 1; I <= m; I ++)
{
Cin> A> B> C;
Map [a] [0] ++;
Map [a] [map [a] [0] = B;
Map [B] [0] ++;
Map [B] [map [B] [0] =;
Flow [a] [B] + = C;
}
}
Void re_d ()
{
Int I, j, a [300], start, final, cur;
Memset (D, 0, sizeof (d ));
Start = 1; Final = 1;
D [s] = 1;
A [1] = s;
While (start <= final)
{
Cur = A [start ++];
For (I = 1; I <= map [cur] [0]; I ++)
{
J = map [cur] [I];
If (flow [cur] [J]> 0)
&&(! D [J])
{
D [J] = d [cur] + 1;
A [++ Final] = J;
}
}
}
}
Int BFS ()
{
Int I, TT [maxn + 1], start, finish, chk [maxn + 1], K;
Memset (TT, 0, sizeof (TT ));
Memset (chk, 0, sizeof (chk ));
Start = 1; finish = 1; TT [1] = s;
While (start <= finish)
{
For (I = 1; I <= map [TT [start] [0]; I ++)
If ((! Chk [map [TT [start] [I]) & (flow [TT [start] [map [TT [start] [I]) & (d [TT [start] + 1 = d [map [TT [start] [I])

// (D [TT [start] + 1 = d [map [TT [start] [I]) add this condition
{
K = map [TT [start] [I];
Finish ++;
TT [finish] = K;
Chk [k] = 1;
Pre [k] = TT [start];
If (k = T) return 1;
}
Start ++;
}
Return 0;
}
Int min (int A, int B)
{
If (A <B) return;
Else return B;
}
Int main ()
{
Int ans, K, V;
While (CIN> m> N)
{
S = 1; t = N;
// CIN> S> T; // s. t indicates the Source and Sink points.
Init ();
Ans = 0;
Re_d ();
While (BFS ())
{
K = T; V = maxflow;
While (K! = S)
{
V = min (v, flow [pre [k] [k]);
K = pre [k];
}
K = T; ans + = V;
While (K! = S)
{
Flow [pre [k] [k]-= V;
Flow [k] [pre [k] + = V;
K = pre [k];
}
Re_d ();
}
Cout <ans <Endl;
}
}

 

// Maximum flow with minimum cost (implemented by Dijkstra)
# Include <stdio. h>
# Include <iostream>
# Include <memory. h>
Using namespace STD;
# Define maxn300
# Define maxflow 2000000
# Define maxv 200000000
Int N, S, T, M, K, flow [maxn + 1] [maxn + 1], map [maxn + 1] [2 * maxn + 1], pre [maxn + 1], d [maxn + 1], numeric [maxn + 1] [maxn + 1], D0 [maxn + 1];
// Flow traffic, watermark value, map saves edge in the form of a record son, the prefix of pre [I] I
Bool Init ()
{
Int I, A, B, J, random, U, V, L, C;
Memset (MAP, 0, sizeof (MAP ));
Memset (flow, 0, sizeof (flow ));
Memset (PRE, 0, sizeof (pre ));
For (I = 0; I <= maxn; I ++)
D [I] = maxv;
For (I = 0; I <= maxn; I ++)
For (j = 0; j <= maxn; j ++)
{
Pipeline [I] [J] = maxv;
}
Cin> N> m> S> T;
If (n = 0) & (M = 0) & (k = 0) return false;
For (I = 1; I <= m; I ++)
{
Cin> A> B> C> quit;
Map [a] [0] ++;
Map [a] [map [a] [0] = B;
Map [B] [0] ++;
Map [B] [map [B] [0] =;
Flow [a] [B] = C;
Pipeline [a] [B] = pipeline;
Pipeline [B] [a] =-pipeline;
}
D [s] = 0;
Return true;
}
Int Dijkstra ()
{
Int I, TT [maxn + 1], chk [maxn + 1], K, min_d, J, min_ I, FT;
Memset (TT, 0, sizeof (TT ));
Memset (chk, 0, sizeof (chk ));
For (I = 0; I <= maxn; I ++)
D [I] = maxv;
D [s] = 0;
FT = 0;
Min_ I = maxv;
For (j = 1; j <= N; j ++)
{
Min_d = maxv;
For (I = 1; I <= N; I ++)
If ((! Chk [I]) & (d [I] <min_d ))
{
Min_ I = I;
Min_d = d [I];
}
If (min_d = maxv) break;
Chk [min_ I] = 1;
For (I = 1; I <= map [min_ I] [0]; I ++)
If (flow [min_ I] [map [min_ I] [I]> 0)
{
K = map [min_ I] [I];
If (d [k]> (d [min_ I] + minute [min_ I] [k]-D0 [min_ I] + D0 [k])
{
D [k] = d [min_ I] + shard [min_ I] [k]-D0 [min_ I] + D0 [k];
Pre [k] = min_ I;
}
}
}
Return (d [T]! = Maxv );
}
Void re_d ()
{
Int I;
For (I = 0; I <= N; I ++) D0 [I] =-d [I];
}
Int min (int A, int B)
{
If (A <B) return;
Else return B;
}
Int main ()
{
Int ans, K, V, St;
While (Init ())
{
Ans = 0;
Memset (D0, 0, sizeof (D0 ));
While (Dijkstra ())
{
K = T; V = maxflow;
While (K! = S)
{
V = min (v, flow [pre [k] [k]);
K = pre [k];
}
K = T; ans + = V * (d [T]-D0 [T]);
While (K! = S)
{
Flow [pre [k] [k]-= V;
Flow [k] [pre [k] + = V;
K = pre [k];
}
Re_d ();
}
Cout <ans <Endl;
}
Return 0;
}

 

 

 

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.