-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathshortest_paths.py
62 lines (56 loc) · 1.68 KB
/
shortest_paths.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
# Uses python3
import sys
def shortet_paths(adj, cost, s, distance, reachable, shortest):
vertices = len(adj)
distance[s] = 0
reachable[s] = 1
queue = []
visited = [False] * vertices
for _ in range(vertices - 1):
for u in range(vertices):
i = 0 # magic variable
for v in adj[u]:
if distance[v] > distance[u] + cost[u][i]:
distance[v] = distance[u] + cost[u][i]
reachable[v] = 1
i += 1
for u in range(vertices):
i = 0 # magic variable
for v in adj[u]:
if distance[v] > distance[u] + cost[u][i]:
if v not in queue:
queue.append(v)
i += 1
while queue:
u = queue.pop(0)
visited[u] = True
shortest[u] = 0
for v in adj[u]:
if not visited[v] and v not in queue:
queue.append(v)
if __name__ == '__main__':
user_input = sys.stdin.read()
data = list(map(int, user_input.split()))
n, m = data[0:2]
data = data[2:]
edges = list(
zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3]))
data = data[3 * m:]
adj = [[] for _ in range(n)]
cost = [[] for _ in range(n)]
for ((a, b), w) in edges:
adj[a - 1].append(b - 1)
cost[a - 1].append(w)
s = data[0]
s -= 1
distance = [float('inf')] * n
reachable = [0] * n
shortest = [1] * n
shortet_paths(adj, cost, s, distance, reachable, shortest)
for x in range(n):
if reachable[x] == 0:
print('*')
elif shortest[x] == 0:
print('-')
else:
print(distance[x])