-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ11779.py
More file actions
75 lines (66 loc) · 1.67 KB
/
BJ11779.py
File metadata and controls
75 lines (66 loc) · 1.67 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import heapq
n = int(sys.stdin.readline())
m = int(sys.stdin.readline())
MAX = sys.maxsize
graph = [[] for _ in range(n + 1)]
dist = [MAX for _ in range(n + 1)]
for _ in range(m):
s, e, c = map(int, sys.stdin.readline().split())
graph[s].append((e, c))
s, e = map(int, sys.stdin.readline().split())
heap = []
heapq.heappush(heap, (0, s, [s]))
dist[s] = 0
while heap:
print(heap)
print(dist)
cost, cur_pos, route = heapq.heappop(heap)
if cur_pos == e:
print(cost)
print(len(route))
print(*route)
break
if dist[cur_pos] < cost:
continue
for next_pos, c in graph[cur_pos]:
if dist[next_pos] > dist[cur_pos] + c:
heapq.heappush(heap, (cost + c, next_pos, route + [next_pos]))
dist[next_pos] = cost + c
# import sys
# import heapq
#
# n = int(sys.stdin.readline())
# m = int(sys.stdin.readline())
# graph = [[] for _ in range(n + 1)]
# dist = [-1 for _ in range(n + 1)]
#
# for _ in range(m):
# s, e, c = map(int, sys.stdin.readline().split())
# graph[s].append([e, c])
#
# s, e = map(int, sys.stdin.readline().split())
#
# heap = []
# heapq.heappush(heap, [0, s, [s]])
# dist[s] = 0
#
# while heap:
# print(heap)
# print(dist)
# cost, cur_pos, route = heapq.heappop(heap)
# if cur_pos == e:
# print(cost)
# print(len(route))
# print(*route)
# break
#
# # if dist[cur_pos]
#
# for next_pos, c in graph[cur_pos]:
# if dist[next_pos] != -1 and dist[next_pos] <= dist[cur_pos] + c:
# continue
#
# heapq.heappush(heap, [cost + c, next_pos, route + [next_pos]])
# dist[next_pos] = dist[cur_pos] + c
#