-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1368.py
38 lines (29 loc) · 846 Bytes
/
1368.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
import sys
import heapq
input = sys.stdin.readline
MAX_COST = 100_000
def solution():
is_well = [False] * (n + 1)
min_heap = [(cost, node) for node, cost in enumerate(costs, start=1)]
heapq.heapify(min_heap)
total_cost = 0
while len(min_heap):
cost, node = heapq.heappop(min_heap)
if is_well[node]:
continue
is_well[node] = True
total_cost += cost
for idx, cost in enumerate(graph[node-1], start=1):
if idx == node:
continue
heapq.heappush(min_heap, (cost, idx))
print(total_cost)
if __name__ == '__main__':
n = int(input())
costs = [MAX_COST] * n
for i in range(n):
costs[i] = int(input())
graph = []
for node in range(n):
graph.append(list(map(int, input().split())))
solution()