Skip to content

Commit 8778dcb

Browse files
committed
solved(python): baekjoon 1967
1 parent cc99655 commit 8778dcb

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

baekjoon/python/1967/__init__.py

Whitespace-only changes.

baekjoon/python/1967/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import heapq
2+
import math
3+
import sys
4+
5+
from collections import defaultdict
6+
7+
read = lambda: sys.stdin.readline().rstrip()
8+
9+
10+
class Problem:
11+
def __init__(self):
12+
self.n = int(read())
13+
14+
self.graph = defaultdict(list)
15+
for src, dest, distance in [map(int, read().split()) for _ in range(self.n - 1)]:
16+
self.graph[src].append((dest, distance))
17+
self.graph[dest].append((src, distance))
18+
19+
def solve(self) -> None:
20+
if self.n == 1:
21+
print(0)
22+
return
23+
24+
distances, target = self.dijkstra(1), (0, 0)
25+
for idx, distance in enumerate(distances):
26+
if distance > target[0]:
27+
target = (distance, idx + 1)
28+
29+
print(max(self.dijkstra(target[1])))
30+
31+
def dijkstra(self, start_vertex: int) -> list[int]:
32+
queue, distances = [(0, start_vertex)], [0 if idx == start_vertex else math.inf for idx in range(self.n + 1)]
33+
34+
while queue:
35+
current_distance, current_vertex = heapq.heappop(queue)
36+
if current_distance > distances[current_vertex]:
37+
continue
38+
39+
for next_vertex, distance in self.graph[current_vertex]:
40+
next_distance = current_distance + distance
41+
if next_distance < distances[next_vertex]:
42+
distances[next_vertex] = next_distance
43+
heapq.heappush(queue, (next_distance, next_vertex))
44+
45+
return list(map(int, distances[1:]))
46+
47+
48+
if __name__ == "__main__":
49+
Problem().solve()

baekjoon/python/1967/sample.json

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

baekjoon/python/1967/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)