Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0fbda09

Browse files
committed
improved some code
1 parentbaabc5c commit0fbda09

File tree

1 file changed

+4
-25
lines changed

1 file changed

+4
-25
lines changed

‎basic_algorithm/graph/mst.md

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
classSolution:
1212
defgetMinRiskValue(self,N,M,X,Y,W):
1313

14-
# Kruskal's algorithm with union-find to construct MST
14+
# Kruskal's algorithm with union-find
1515
parent=list(range(N+1))
1616

1717
deffind(x):
@@ -29,30 +29,9 @@ class Solution:
2929

3030
edges=sorted(zip(W, X, Y))
3131

32-
MST_edges= []
33-
for edgein edges:
34-
if union(edge[1], edge[2]):
35-
MST_edges.append(edge)
36-
if find(0)== find(N):
37-
break
38-
39-
MST= collections.defaultdict(list)
40-
target= find(0)
41-
for w, u, vinMST_edges:
42-
if find(u)== targetand find(v)== target:
43-
MST[u].append((v, w))
44-
MST[v].append((u, w))
45-
46-
# dfs to search route from 0 to n
47-
dfs= [(0,None,float('-inf'))]
48-
while dfs:
49-
v, p, max_w= dfs.pop()
50-
for n, winMST[v]:
51-
cur_max_w=max(max_w, w)
52-
if n== N:
53-
return cur_max_w
54-
if n!= p:
55-
dfs.append((n, v, cur_max_w))
32+
for w, x, yin edges:
33+
if union(x, y)and find(0)== find(N):# early return without constructing MST
34+
return w
5635
```
5736

5837
```Python

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp