ecnu 1817 Shortest path

Posted taming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ecnu 1817 Shortest path相关的知识,希望对你有一定的参考价值。

这里用python的类来封装了下,纯属娱乐

class shortestpath():
    def __init__(self,x):
        self.n=x
        self.dis=[[float(inf) for i in range(0,x+1)] for j in range(0,x+1)]
        for i in range(0,x+1):
            self.dis[i][i]=0

    def dijkstra(self,s):
        lowdis=[float(inf)]*(self.n+1)
        vis=[False]*(self.n+1)
        vis[s]=True
        for i in range(0,self.n+1):
            lowdis[i]=self.dis[s][i]
        
        for i in range(1,self.n):
            mind=float(inf)
            minn=-1
            for j in range(0,self.n+1):
                if not vis[j]:
                    if lowdis[j]<mind:
                        mind=lowdis[j]
                        minn=j
            if mind<float(inf):
                vis[minn]=True
                for j in range(0,self.n+1):
                    if not vis[j]:
                        if mind+self.dis[minn][j]<lowdis[j]:
                            lowdis[j]=mind+self.dis[minn][j]
            else: return lowdis
        return lowdis

n,m=map(int,input().split())
d=shortestpath(n)
for i in range(0,m):
    u,v,w=map(int,input().split())
    if d.dis[u][v]>w:
        d.dis[u][v]=w

lowdis=d.dijkstra(1)
if lowdis[n]!=float(inf):
    print(lowdis[n])
else: print("-1")

 

以上是关于ecnu 1817 Shortest path的主要内容,如果未能解决你的问题,请参考以下文章

使用labeled_graph 提升图bellman_ford_shortest_paths

LeetCode 847. Shortest Path Visiting All Nodes的强化学习解法

干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和

6-17 Shortest Path [4] (25分)