python实现----dijkstra优先队列

众所周知,求无负权的最短路问题使用dijkstra算法解决,其可以用优先队列优化,这里给出python实现的版本

import time
import heapq

class Node:

     def __init__(self, name):
        self.name = name
        self.vis = False
        self.adjacenciesList = []
        self.pre = None
        self.dis = 0x3f3f3f3f    

     def __lt__(self, other):
        return self.dis < other.dis

class Edge:

    def __init__(self, weight, startvertex, endvertex):
        self.weight = weight
        self.startvertex = startvertex
        self.endvertex = endvertex

def calculateshortestpath(vertexlist, startvertex):
    q = []
    startvertex.dis = 0
    heapq.heappush(q, startvertex)

    while q:
        tmp = heapq.heappop(q)
        for edge in tmp.adjacenciesList:
            tempdist = edge.startvertex.dis + edge.weight
            if tempdist < edge.endvertex.dis:
                edge.endvertex.dis = tempdist
                edge.endvertex.pre = edge.startvertex
                heapq.heappush(q,edge.endvertex)
def getshortestpath(t):
    print("The value of it's minimum distance is: ",t.dis),
    node = t
    while node:
        print(node.name),
        node = node.pre
    print(" ")
#主函数部分
node=[]
edge=[]
n=int(raw_input())
m=int(raw_input())
for i in range(n):
     node.append(Node(i+1)) #生成节点的名字
for i in range(m):
     a,b,c = map(int, raw_input().split())  #a到b的距离为c
     edge.append(Edge(c,node[a-1],node[b-1]))
     node[a-1].adjacenciesList.append(edge[i])
begin=time.clock()
calculateshortestpath(node,node[0])
for i in range(n):
 getshortestpath(node[i])
end=time.clock()
end-=begin
print(" ")
print("read :%f s" % end)


    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/imotolove/article/details/80633006
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞