-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10423.py
51 lines (37 loc) · 921 Bytes
/
10423.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def find_parent(parent, x):
if parent[x] == -1:
return -1
if x == parent[x]:
return x
return find_parent(parent,parent[x])
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
def check(parent):
for i in parent:
if i != -1:
return False
return True
n, m ,k = map(int, input().split(" "))
spots = list(map(int, input().split(" ")))
parent = [i for i in range(n + 1)]
result = 0
for i in spots:
parent[i] = -1
edges = []
for i in range(m):
a,b,cost = map(int, input().split(" "))
edges.append((cost, a, b))
edges.sort()
for edge in edges:
cost, a, b = edge
if find_parent(parent, a) != find_parent(parent, b):
union_parent(parent,a,b)
result += cost
if check(parent):
break
print(result)