晚风


私信TA

用户名:uq_24611172404

访问量:2117

签 名:

等  级
排  名 5198
经  验 1517
参赛次数 0
文章发表 35
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

import heapq

N=100010

e=[0]*N

ne=[0]*N

w=[0]*N

h=[-1]*N

index=0

st=[False]*N


def add(a,b,c):

    global index

    e[index]=b

    w[index]=c

    ne[index]=h[a]

    h[a]=index

    index+=1

def dijkstra():

    dis=[float('inf')]*(n+1)

    dis[1]=0

    heap=[]

    heapq.heappush(heap,(dis[1],1))

    while heap:

        t=heapq.heappop(heap)

        distance,ver=t[0],t[1]

        if st[ver]==True:

            continue

        st[ver]=True

        i=h[ver]

        while i!=-1:

            j=e[i]

            if dis[j]>distance+w[i]:

                dis[j]=distance+w[i]

                heapq.heappush(heap,(dis[j],j))

            i=ne[i]

    if dis[n]==float('inf'): return -1

    else:return dis[n]

    

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

for i in range(m):

    a,b,c=map(int,input().split())

    add(a,b,c)

    add(b,a,c)

print(dijkstra())


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区