This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evacuation.py
238 lines (200 loc) · 5.04 KB
/
evacuation.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# python3
##############################
#
# @author Daniel Avdar
#
# @description:In this problem, you will apply an algorithm for finding
# maximum flow in a network to determine how fast people can
# be evacuated from the given city.
#
# @input: a flow network
#
# @output:Output a single integer — the maximum number of people
# that can be evacuated from the city number 1 each hour.
#
# @Constraints:1≤𝑛≤100; 0≤𝑚≤10 000; 1≤𝑢, 𝑣≤𝑛; 1≤𝑐≤10 000. It is guaranteed
# that 𝑚·EvacuatePerHour≤2·108, where EvacuatePerHour is the
# maximum number of people that can be evacuated from the
# city each hour — the number which you need to output.
#
##############################
import queue
class Edge:
def __init__(self, u, v, capacity, not_rev=True):
self.from_ = u
self.to = v
self.capacity = capacity
self.flow = 0
self.not_rev = not_rev
class FlowGraph:
def __init__(self, n):
self.edges = []
self.graph = [[] for _ in range(n)]
def add_edge(self, from_, to, capacity):
forward_edge = Edge(from_, to, capacity)
backward_edge = Edge(to, from_, 0, False)
self.graph[from_].append(len(self.edges))
self.edges.append(forward_edge)
self.graph[to].append(len(self.edges))
self.edges.append(backward_edge)
def size(self):
return len(self.graph)
def get_ids(self, from_):
return self.graph[from_]
def get_edge(self, id):
return self.edges[id]
def add_flow(self, id, flow):
self.edges[id].flow += flow
self.edges[id ^ 1].flow -= flow
def read_data():
vertex_count, edge_count = map(int, input().split())
graph = FlowGraph(vertex_count)
for _ in range(edge_count):
u, v, capacity = map(int, input().split())
graph.add_edge(u - 1, v - 1, capacity)
return graph
def get_bottleneck(graph, path, to):
ed = graph.get_edge(path[to])
bottleneck = float('inf')
while ed is not None:
bottleneck = min(bottleneck, ed.capacity - ed.flow)
if path[ed.from_] is None:
break
ed = graph.get_edge(path[ed.from_])
return bottleneck
def add_flow_to_path(graph, path, to, bottleneck):
ed = graph.get_edge(path[to])
c_id = path[to]
while ed is not None:
graph.add_flow(c_id, bottleneck)
c_id = path[ed.from_]
if path[ed.from_] is None:
break
ed = graph.get_edge(path[ed.from_])
def bfs(graph, from_, to):
n = graph.size()
path = [None] * n
q = queue.Queue()
q.put(from_)
while not q.empty():
curr_node_id = q.get()
for id in graph.get_ids(curr_node_id):
curr_edge = graph.get_edge(id)
not_done_yet = path[curr_edge.to] is None and \
from_ != curr_edge.to and \
curr_edge.capacity - curr_edge.flow > 0
if not_done_yet:
path[curr_edge.to] = id
q.put(curr_edge.to)
if path[to] is None:
return None
return path
def max_flow(graph, from_, to):
flow = 0
while True:
path = bfs(graph, from_, to)
if path is None:
break
bottleneck = get_bottleneck(graph, path, to)
add_flow_to_path(graph, path, to, bottleneck)
flow += bottleneck
es = graph.get_ids(from_)
sum_ = 0
for i in es:
e = graph.get_edge(i)
sum_ += e.flow
return flow
def test(filename):
"""
>>> test("01")
1
>>> test("02")
1
>>> test("03")
1
>>> test("04")
1
>>> test("05")
1
>>> test("06")
1
>>> test("07")
1
>>> test("08")
1
>>> test("09")
1
>>> test("10")
1
>>> test("11")
1
>>> test("12")
1
>>> test("13")
1
>>> test("14")
1
>>> test("15")
1
>>> test("16")
1
>>> test("17")
1
>>> test("18")
1
>>> test("19")
1
>>> test("20")
1
>>> test("21")
1
>>> test("22")
1
>>> test("23")
1
>>> test("24")
1
>>> test("25")
1
>>> test("26")
1
>>> test("27")
1
>>> test("28")
1
>>> test("29")
1
>>> test("30")
1
>>> test("31")
1
>>> test("32")
1
>>> test("33")
1
>>> test("34")
1
>>> test("35")
1
>>> test("36")
1
"""
f = open(filename)
lines = f.readlines()
vertex_count, edge_count = map(int, lines[0].split())
graph = FlowGraph(vertex_count)
for i in lines[1:]:
u, v, capacity = map(int, i.split())
graph.add_edge(u - 1, v - 1, capacity)
f.close()
f = open(filename + ".a")
lines = f.readlines()
expected = int(lines[0].split()[0])
f.close()
res = max_flow(graph, 0, graph.size() - 1)
pass_ = res == expected
return 1 if pass_ else res
if __name__ == '__main__':
graph = read_data()
print(max_flow(graph, 0, graph.size() - 1))
# todo git