HEAD
def find_lowest_cost_node(costs): ======= 星星之火,可以燎原
def find_lowest_cost_node(costs): >>>>>>> 4ef99384a791603d1ae54a12162221aa23eb330e lowest_cost = float("inf") lowest_cost_node = None for node in costs: cost = costs[node] if cost < lowest_cost and node not in processed: lowest_cost = cost lowest_cost_node = node return lowest_cost_node # 定义节点信息 graph = dict() graph["start"] = {} graph["start"]["a"] = 6 graph["start"]["b"] = 2 graph["a"] = {} graph["a"]["fin"] = 1 graph["b"] = {} graph["b"]["a"] = 3 graph["b"]["fin"] = 5 graph["fin"] = {} # 开销表 infinity = float("inf") costs = dict() costs["a"] = 6 costs["b"] = 2 costs["fin"] = infinity # 存储父节点 parents = dict() parents["a"] = "start" parents["b"] = "start" parents["fin"] = None # 记录处理过的节点 processed = [] # 操作流程 node = find_lowest_cost_node(costs) while node is not None: cost = costs[node] neighbors = graph[node] for n in neighbors.keys(): new_cost = cost + neighbors[n] if costs[n] > new_cost: costs[n] = new_cost parents[n] = node processed.append(node) node = find_lowest_cost_node(costs) print(costs["fin"])