Skip to content

Commit a8a3964

Browse files
committed
solved(python): baekjoon 2887
1 parent 6fe4a86 commit a8a3964

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

baekjoon/python/2887/__init__.py

Whitespace-only changes.

baekjoon/python/2887/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.n = int(read())
9+
self.planets = []
10+
11+
for idx in range(self.n):
12+
x, y, z = map(int, read().split())
13+
self.planets.append((idx, x, y, z))
14+
15+
self.roots = [x for x in range(self.n)]
16+
17+
def solve(self) -> None:
18+
print(self.find_minimum_cost())
19+
20+
def find_minimum_cost(self) -> int:
21+
edges, weight = self.find_edges(), 0
22+
23+
for distance, src, dest in edges:
24+
if self.union(src, dest):
25+
weight += distance
26+
27+
return weight
28+
29+
def find_edges(self) -> list[tuple[int, int, int]]:
30+
edges = []
31+
for axis in range(1, 4):
32+
self.planets.sort(key=lambda planet: planet[axis])
33+
34+
for idx in range(self.n - 1):
35+
edges.append(
36+
(
37+
abs(self.planets[idx][axis] - self.planets[idx + 1][axis]),
38+
self.planets[idx][0],
39+
self.planets[idx + 1][0],
40+
)
41+
)
42+
43+
return sorted(edges)
44+
45+
def union(self, src: int, dest: int) -> bool:
46+
src_root, dest_root = self.find_root(src), self.find_root(dest)
47+
if src_root == dest_root:
48+
return False
49+
50+
self.roots[src_root] = dest_root
51+
return True
52+
53+
def find_root(self, num: int) -> int:
54+
while num != self.roots[num]:
55+
self.roots[num] = self.roots[self.roots[num]]
56+
num = self.roots[num]
57+
58+
return num
59+
60+
61+
if __name__ == "__main__":
62+
Problem().solve()

baekjoon/python/2887/sample.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"input": [
4+
"5",
5+
"11 -15 -15",
6+
"14 -5 -15",
7+
"-1 -1 -5",
8+
"10 -4 -1",
9+
"19 -4 19"
10+
],
11+
"expected": [
12+
"4"
13+
]
14+
}
15+
]

baekjoon/python/2887/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)