We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fab53e6 commit 84efec7Copy full SHA for 84efec7
👑 G4/13905.py
@@ -0,0 +1,35 @@
1
+import sys
2
+input = sys.stdin.readline
3
+sys.setrecursionlimit(10**6)
4
+
5
+n, m = map(int, input().split())
6
+s, e = map(int, input().split())
7
8
+edges = []
9
+mst = []
10
+for _ in range(m):
11
+ a, b, c = map(int, input().split())
12
+ edges.append([c, a, b])
13
+edges.sort(reverse=True, key=lambda x:x[0])
14
15
+parent = [i for i in range(n+1)]
16
17
+def find(x):
18
+ if x == parent[x]: return x
19
+ parent[x] = find(parent[x])
20
+ return parent[x]
21
22
+def union(x, y):
23
+ x = find(x)
24
+ y = find(y)
25
+ if x != y: parent[x] = y
26
27
+for c, a, b in edges:
28
+ if find(a) == find(b): continue
29
+ mst.append(c)
30
+ union(a, b)
31
+ if find(s) == find(e):
32
+ print(min(mst))
33
+ break
34
+else:
35
+ print(0)
0 commit comments