File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -451,6 +451,33 @@ public class Main {
451451```
452452
453453### Python
454+ ``` Python
455+ def main ():
456+ n, m = map (int , input ().strip().split())
457+ edges = []
458+ for _ in range (m):
459+ src, dest, weight = map (int , input ().strip().split())
460+ edges.append([src, dest, weight])
461+
462+ minDist = [float (" inf" )] * (n + 1 )
463+ minDist[1 ] = 0 # 起点处距离为0
464+
465+ for i in range (1 , n):
466+ updated = False
467+ for src, dest, weight in edges:
468+ if minDist[src] != float (" inf" ) and minDist[src] + weight < minDist[dest]:
469+ minDist[dest] = minDist[src] + weight
470+ updated = True
471+ if not updated: # 若边不再更新,即停止回圈
472+ break
473+
474+ if minDist[- 1 ] == float (" inf" ): # 返还终点权重
475+ return " unconnected"
476+ return minDist[- 1 ]
477+
478+ if __name__ == " __main__" :
479+ print (main())
480+ ```
454481
455482### Go
456483
You can’t perform that action at this time.
0 commit comments