Submission #3009015


Source Code Expand

import heapq

n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
ixy = [(i, x, y) for i, (x, y) in enumerate(xy)]

# Get edges with cost along both x, y
edges = [[] for i in range(n)]
for x_or_y in [1, 2]:
    xy_sorted = sorted(ixy, key = lambda a: a[x_or_y])
    for i in range(n-1):
        i1 = xy_sorted[i][0]
        i2 = xy_sorted[i+1][0]
        cost = xy_sorted[i+1][x_or_y] - xy_sorted[i][x_or_y]
        edges[i1].append((i2, cost))
        edges[i2].append((i1, cost))

# Calculate minimum Spanning tree (Prim algorithm)
distinct = [False]*n
ans = 0
pque = []
heapq.heappush(pque, (0, 0))
while len(pque) > 0:
    # print(pque)
    cost, v = heapq.heappop(pque)
    if distinct[v]: continue
    
    ans += cost
    distinct[v] = True

    for v_next, cost_next in edges[v]:
        if not distinct[v_next]:
            heapq.heappush(pque, (cost_next, v_next))

print(ans)

Submission Info

Submission Time
Task D - Built?
User tapyoka
Language Python (3.4.3)
Score 500
Code Size 942 Byte
Status AC
Exec Time 1634 ms
Memory 86116 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 28
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 1548 ms 86108 KB
02.txt AC 1634 ms 86076 KB
03.txt AC 1554 ms 86116 KB
04.txt AC 1558 ms 86064 KB
05.txt AC 1524 ms 86108 KB
06.txt AC 1592 ms 86052 KB
07.txt AC 1561 ms 86092 KB
08.txt AC 1519 ms 86084 KB
09.txt AC 1524 ms 85320 KB
10.txt AC 1512 ms 85204 KB
11.txt AC 1487 ms 85312 KB
12.txt AC 1580 ms 85324 KB
13.txt AC 1334 ms 79728 KB
14.txt AC 1293 ms 81392 KB
15.txt AC 1172 ms 72948 KB
16.txt AC 1177 ms 72800 KB
17.txt AC 1252 ms 79208 KB
18.txt AC 1251 ms 79112 KB
19.txt AC 1146 ms 72808 KB
20.txt AC 1448 ms 79776 KB
21.txt AC 1299 ms 73628 KB
22.txt AC 1258 ms 72940 KB
23.txt AC 780 ms 66364 KB
24.txt AC 1292 ms 71356 KB
25.txt AC 18 ms 3064 KB
26.txt AC 18 ms 3064 KB
s1.txt AC 18 ms 3064 KB
s2.txt AC 18 ms 3064 KB