ecnu 1817 Shortest Path

Posted taming

tags:

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

def dijkstra(N,lowdis,dis):
    vis=[]
    for i in range(0,N): #有N个节点编号为0~N-1
        lowdis.append(dis[0][i])
        vis.append(False)
    vis[0]=True
    for i in range(1,N):
        mind=float(inf)
        minn=-1 
        for j in range(0,N):
            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,N):
                if not vis[j]:
                    if lowdis[j]>mind+dis[minn][j]:
                        lowdis[j]=mind+dis[minn][j]
        else:
            return -1
    return lowdis[N-1]

n,m=map(int,input().split())

lowdis=[]
dis=[]
line=[]

for i in range(0,n):
    line.append(float(inf))
for i in range(0,n):
    dis.append(line[:])
    dis[i][i]=0
for i in range(0,m):
    a,b,c=map(int,input().split())
    if dis[a-1][b-1]>c:
        dis[a-1][b-1]=c
    
print(dijkstra(n,lowdis,dis))

 

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

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分)

6-16 Shortest Path [3] (25分)