# 第一题应该是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
点赞 评论

相关推荐

头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
重生2012之我是java程序员:换个稍微正式点的照片吧
点赞 评论 收藏
分享
牛客网
牛客企业服务