-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
52 lines (40 loc) · 1.39 KB
/
example.py
File metadata and controls
52 lines (40 loc) · 1.39 KB
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
from sys import stdin
#### Paste floyd_warshall.py source code here ####
class FloydWarshall: ...
def main():
readline = stdin.readline
N, M, Q = map(int, readline().split())
edges = []
for _ in range(M):
A, B, C = map(int, readline().split())
edges.append((A - 1, B - 1, C))
queries = []
edge_enabled = [True] * M
for _ in range(Q):
com, *args = map(int, readline().split())
if com == 1:
edge_idx = args[0] - 1
queries.append((com, edge_idx))
edge_enabled[edge_idx] = False
else:
x, y = args
queries.append((com, (x - 1, y - 1)))
fw = FloydWarshall(N, inf=N * 10**9)
for edge_idx, (A, B, C) in enumerate(edges):
if edge_enabled[edge_idx]:
fw.add_edge(A, B, C)
fw.add_edge(B, A, C)
fw.solve() # dist matrix is now valid; update_dists=True becomes available
answers = []
for com, query in reversed(queries):
if com == 1:
A, B, C = edges[query]
fw.add_edge(A, B, C, update_dists=True)
fw.add_edge(B, A, C, update_dists=True)
else:
x, y = query
d = fw.dist[x][y]
answers.append(-1 if d == fw.inf else d)
print("\n".join(map(str, reversed(answers))))
if __name__ == "__main__":
main()