-
Notifications
You must be signed in to change notification settings - Fork 0
/
cte.py
34 lines (26 loc) · 1000 Bytes
/
cte.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
# Shortest Cycle Through a Given Edge
from .helpers import parse_graphs
from .dij import dij
# For this problem, we start Dijaktra's search at the end of the specified
# edge, and take the distance to the start of the edge (if reachable) and add
# the weight of the specified edge to get the cycle length.
def first_edges(handle):
lines = handle.read().splitlines()
ngraphs = int(lines[0])
start = 1
edges = []
for i in range(ngraphs):
if lines[start] == "":
start += 1
_, n_edges = lines[start].split()
edges += [list(map(int, lines[start + 1].split()))]
start += int(n_edges) + 1
return edges
def cte(graph, edge):
dist = dij(graph, start=edge[1])[edge[0] - 1]
return dist if dist == -1 else dist + edge[2]
def main(file):
edges = first_edges(open(file))
graphs = list(parse_graphs(open(file), directed=True, weighted=True))
res = [cte(graphs[i], edges[i]) for i in range(len(graphs))]
print(*res)