Python实现Dijkstra算法

描述

地图上有 m 条无向边,每条边 (x, y, w) 表示位置 x 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。

请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?

  • 1 <= m <= 500,1 <= n <= 50,0 <= x, y <= n,1 <= w <= 100000。
  • 题目保证位置 0 一定可以通往位置 n。
  • 题目保证地图中不包含重边。

样例
样例 1:

给出 n = 2, m = 2, x = [0, 1], y = [1, 2], w = [1, 2],返回2
输入:
2
2
[0,1]
[1,2]
[1,2]
输出:
2

解释:
路径 1:0->1->2(危险值:2)
最小危险值为2。

样例 2:

给出 n = 3, m = 4, x = [0, 0, 1, 2], y = [1, 2, 3, 3], w = [1, 2, 3, 4],返回3
输入:
3
4
[0,0,1,2]
[1,2,3,3]
[1,2,3,4]
输出:
3

解释:
路径 1:0->1->3(危险值:3)
路径 2:0->2->3(危险值:4)
最小危险值为3。

class Solution:
    """
    @param n: maximum index of position.
    @param m: the number of undirected edges.
    @param x: 
    @param y: 
    @param w: 
    @return: return the minimum risk value.
    """
    def getMinRiskValue(self, n, m, x, y, w):
        start = 0  #起点编号
        end = n  #终点编号


        #构建一个邻接散列表
        graph = {}
        for i,j,k in zip(x,y,w):
            if i not in graph.keys():
                graph[i] = {}
            graph[i][j] = k
        graph[end] = {}


        #构建一个散列表存储从起点到该节点的开销
        Inf = float('inf')
        costs = {}
        for i,j,k in zip(x,y,w):
            if i == start:
                costs[j] = k
            else:
                costs[j] = Inf

                
        #构建一个散列表存储各节点的父节点,用于记录路径
        parents = {}
        for i,j,k in zip(x,y,w):
            if i == start:
                parents[j] = start
            else:
                parents[j] = None

                
        #构建一个数组,记录处理过的节点
        processed = []

        #构建一个函数,在未处理的节点中找出开销最小的节点
        def find_lowest_cost_node(costs):
            lowest_cost = 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

        node = find_lowest_cost_node(costs)
        while node is not None:
            cost = costs[node]
            neighbors = graph[node]
            for n in neighbors.keys():
                new_cost = max([cost,neighbors[n]])
                if costs[n] > new_cost:
                    costs[n] = new_cost
                    parents[n] = node
            processed.append(node)
            node = find_lowest_cost_node(costs)
        #获取最小危险值
        cost = costs[end]
        return cost
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天为大家分享的算法是为解决最短路径算法的Dijkstra算法(简称D算法),这是一个解决从点到点之间最短路径的问...
    电击小子_ea1b阅读 12,027评论 0 2
  • 1.链表 1.实现一个单向链表 2.找出链表相交节点,假设均没有环 3.判断链表是否有环思路:使用快慢两个指针,当...
    X1028阅读 3,919评论 0 0
  • Problem G是一个n个顶点和e条边的带权有向图,各边的权值为0到N-1之间的整数,N为一非负整数。修改Dij...
    1Z实验室阿凯阅读 8,098评论 0 2
  • ----------------------- 页面 1----------------------- 2013 ...
    长春傳齐3阅读 10,390评论 0 1
  • 问题描述 一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”。例如求下图中的1号顶点到2、3、4、5、...
    一书文集阅读 3,704评论 0 0