-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmin-cost-max-flow.py
171 lines (123 loc) · 3.46 KB
/
min-cost-max-flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Python3 program to implement
from sys import maxsize
from typing import List
# Stores the found edges
found = []
# Stores the number of nodes
N = 0
# Stores the capacity
# of each edge
cap = []
flow = []
# Stores the cost per
# unit flow of each edge
cost = []
# Stores the distance from each node
# and picked edges for each node
dad = []
dist = []
pi = []
INF = maxsize // 2 - 1
# Function to check if it is possible to
# have a flow from the src to sink
def search(src: int, sink: int) -> bool:
# Initialise found[] to false
found = [False for _ in range(N)]
# Initialise the dist[] to INF
dist = [INF for _ in range(N + 1)]
# Distance from the source node
dist[src] = 0
# Iterate untill src reaches N
while src != N:
best = N
found[src] = True
for k in range(N):
# If already found
if found[k]:
continue
# Evaluate while flow
# is still in supply
if flow[k][src] != 0:
# Obtain the total value
val = dist[src] + pi[src] - pi[k] - cost[k][src]
# If dist[k] is > minimum value
if dist[k] > val:
# Update
dist[k] = val
dad[k] = src
if flow[src][k] < cap[src][k]:
val = dist[src] + pi[src] - pi[k] + cost[src][k]
# If dist[k] is > minimum value
if dist[k] > val:
# Update
dist[k] = val
dad[k] = src
if dist[k] < dist[best]:
best = k
# Update src to best for
# next iteration
src = best
for k in range(N):
pi[k] = min(pi[k] + dist[k], INF)
# Return the value obtained at sink
return found[sink]
# Function to obtain the maximum Flow
def getMaxFlow(
capi: List[List[int]], costi: List[List[int]], src: int, sink: int
) -> List[int]:
global cap, cost, found, dist, pi, N, flow, dad
cap = capi
cost = costi
N = len(capi)
found = [False for _ in range(N)]
flow = [[0 for _ in range(N)] for _ in range(N)]
dist = [INF for _ in range(N + 1)]
dad = [0 for _ in range(N)]
pi = [0 for _ in range(N)]
totflow = 0
totcost = 0
# If a path exist from src to sink
while search(src, sink):
# Set the default amount
amt = INF
x = sink
while x != src:
amt = min(
amt,
flow[x][dad[x]]
if (flow[x][dad[x]] != 0)
else cap[dad[x]][x] - flow[dad[x]][x],
)
x = dad[x]
x = sink
while x != src:
if flow[x][dad[x]] != 0:
flow[x][dad[x]] -= amt
totcost -= amt * cost[x][dad[x]]
else:
flow[dad[x]][x] += amt
totcost += amt * cost[dad[x]][x]
x = dad[x]
totflow += amt
# Return pair total cost and sink
return [totflow, totcost]
# Driver Code
if __name__ == "__main__":
s = 0
t = 4
cap = [
[0, 3, 1, 0, 3],
[0, 0, 2, 0, 0],
[0, 0, 0, 1, 6],
[0, 0, 0, 0, 2],
[0, 0, 0, 0, 0],
]
cost = [
[0, 1, 0, 0, 2],
[0, 0, 0, 3, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
]
ret = getMaxFlow(cap, cost, s, t)
print("{} {}".format(ret[0], ret[1]))