### King-James-23's blog

By King-James-23, history, 4 weeks ago,

class graph:#dijkstra def init(self,size): self.adj=[[0]*size for i in range(size)] self.d=['']*size self.size=size def data(self,data,num): if num>=0 and num<self.size: self.d[num]=data def edge(self,q,w,km): self.adj[q][w]=km def find(self,start): ind=self.d.index(start) visited=[False]*self.size distances=[float("inf")]*self.size distances[ind]=0 for i in range(self.size): mi=float("inf") q=None for ii in range(self.size): if not visited[ii] and distances[ii]<mi: mi=distances[ii] q=ii if q==None: break; for ii in range(self.size): if self.adj[q][ii]!=0 and not visited[ii]: w=distances[q]+self.adj[q][ii] if w<distances[ii]: distances[ii]=w visited[q]=True return distances m=graph(6) m.data('A',0) m.data('B',1) m.data('C',2) m.data('D',3) m.data('E',4) m.data('F',5) m.edge(0,1,1) m.edge(0,2,4) m.edge(0,3,2) m.edge(1,2,6) m.edge(2,4,3) m.edge(3,4,4) m.edge(3,5,7) m.edge(4,5,2) dis=m.find('A') for i, dd in enumerate(dis): print(f"Distance from A to {m.d[i]}: {dd}") print(dis)

• -1