难度指数:⭐⭐⭐⭐⭐
知识点: Dijkstra
2023华为OD机试真题B卷 Python 实现【最小传输时延I】【100%通过率】
Mine:
代码实现
import functools
from time import time
def solve():
return 0
def solve0():
refine = [float('inf') for _ in range(N + 1)]
refine[start] = 0
queue = [[start, 0]]
while len(queue):
node, d = queue.pop(0)
for i in times:
if i[0] == node:
d_temp = refine[node] + i[2]
if d_temp < refine[i[1]]:
refine[i[1]] = d_temp
queue.append([i[1], refine[i[1]]])
queue.sort(key = lambda x: x[1])
if refine[end] == float('inf'):
res = -1
else:
res = refine[end]
return res
T = 1
while T:
N, M = list(map(int, input().split(' ')))
times = [list(map(int, input().split(' '))) for _ in range(M)]
start, end = list(map(int, input().split(' ')))
t1 = time()
res = solve()
t2 = time()
res0 = solve0()
t3 = time()
print(res, res0)
print(t3 - t2, t2 - t1)
T -= 1