标签:sel col else bsp TE split print elf als
这里用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")
标签:sel col else bsp TE split print elf als
原文地址:https://www.cnblogs.com/TAMING/p/9126366.html