关注
# 第一题应该是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
查看原帖
点赞 评论
相关推荐
牛客热帖
更多
正在热议
更多
# 实习生的蛐蛐区 #
1008311次浏览 5136人参与
# 扒一扒那些奇葩实习经历 #
160784次浏览 1183人参与
# 发面经攒人品 #
8907194次浏览 98772人参与
# 应届生第一份工资要多少合适 #
28316次浏览 108人参与
# 27届实习投递记录 #
166793次浏览 1684人参与
# 应届生,你找到工作了吗 #
181065次浏览 914人参与
# 招聘要求与实际实习内容不符怎么办 #
226905次浏览 1077人参与
# 机械人值得去的小众企业 #
38420次浏览 68人参与
# 现在入门AI首先要做什么? #
18352次浏览 145人参与
# 互联网行业现在还值得去吗 #
65745次浏览 380人参与
# 实习最想跑路的瞬间 #
147764次浏览 787人参与
# 面试反问你会问什么 #
213684次浏览 1962人参与
# 机械人,秋招第一次笔试的企业是哪家? #
106994次浏览 715人参与
# 万物皆可发面经 #
5661次浏览 67人参与
# AI了,我在打一种很新的工 #
211832次浏览 2356人参与
# 实习,不懂就问 #
231978次浏览 1771人参与
# 实习教会我的事 #
82342次浏览 521人参与
# 网易求职进展汇总 #
218871次浏览 1542人参与
# 春招前还要继续实习吗? #
72154次浏览 353人参与
# 校招求职有谈薪空间吗 #
234517次浏览 2400人参与
