关注
# 第一题应该是dijkstra's algorithm的变种?没来得及仔细debug,通过率0%。第二题看起来简单,到交卷了才发现编号只能0-9,通过率0% (此处应该微笑
def minTravelTime(intersections, roads, start, goal):
frontier = []
frontier.append([start, 0])
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
t = 0
while len(frontier) > 0:
current = min(frontier, key=lambda x: x[1])
frontier.remove(current)
current = current[0]
if current == goal:
print(current)
return cost_so_far(current)
for next in get_neighbors(current, roads):
wait_time = get_wait_time(next[0], intersections, t)
new_cost = cost_so_far[current] + wait_time + next[2]
t = t + wait_time + next[2]
if next[1] not in cost_so_far or new_cost < cost_so_far[next[1]]:
cost_so_far[next[1]] = new_cost
frontier.append([next[1], new_cost])
came_from[next] = current
def get_neighbors(node, roads):
results = []
for road in roads:
if node == roads[0]:
results.append(road)
return results
def get_wait_time(node, intersection, t):
change = intersection[node]
if (t/change)%2 == 0:
return 0
else:
return ((t/change)+1)*change - t
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# i人适合做什么工作 #
4624次浏览 59人参与
# 大家实习每天都在干啥 #
95459次浏览 532人参与
# “vivo”个offer #
27275次浏览 201人参与
# 如果秋招能重来,我会____ #
20504次浏览 192人参与
# 我是面试官,请用一句话让我破防 #
5325次浏览 52人参与
# 如果上班像打游戏,你最想解锁什么技能 #
3816次浏览 43人参与
# 快手技术岗信息交流阵地 #
13803次浏览 80人参与
# 你认为哪些项目算烂大街? #
72627次浏览 603人参与
# 许愿池 #
320775次浏览 2905人参与
# 校招生月薪1W算什么水平 #
7167次浏览 51人参与
# 硬件应届生薪资是否普遍偏低? #
87939次浏览 558人参与
# 华为池子有多大 #
105163次浏览 739人参与
# 苦尽甘来时,再讲来时路 #
19563次浏览 302人参与
# 作业帮求职进展汇总 #
70339次浏览 484人参与
# 一份好的简历长什么样? #
10116次浏览 236人参与
# 为了实习逃课值吗? #
16492次浏览 146人参与
# 你认为小厂实习有用吗? #
94616次浏览 609人参与
# 秋招许愿,本周能____ #
19617次浏览 134人参与
# 班味很重的人是啥样的? #
5904次浏览 39人参与
# 投递无反馈,如何优化求职策略? #
3315次浏览 32人参与
# 大学最后一个寒假,我想…… #
62079次浏览 667人参与
# 机械制造秋招总结 #
83862次浏览 825人参与


