Skip to content

Commit 6235b0e

Browse files
committed
solved(python): baekjoon 1504
1 parent 94896cc commit 6235b0e

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

baekjoon/python/1504/__init__.py

Whitespace-only changes.

baekjoon/python/1504/main.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import heapq
2+
import sys
3+
4+
from collections import defaultdict
5+
6+
read = lambda: sys.stdin.readline().rstrip()
7+
8+
9+
class Problem:
10+
def __init__(self):
11+
self.n, self.e = map(int, read().split())
12+
13+
self.graph = defaultdict(list)
14+
for src, dest, distance in [map(int, read().split()) for _ in range(self.e)]:
15+
self.graph[src - 1].append((dest - 1, distance))
16+
self.graph[dest - 1].append((src - 1, distance))
17+
18+
self.stopover = list(map(lambda x: int(x) - 1, read().split()))
19+
20+
def solve(self) -> None:
21+
print(
22+
min(
23+
self.calculate_total_distance(
24+
[
25+
(0, self.stopover[0]),
26+
(self.stopover[0], self.stopover[1]),
27+
(self.stopover[1], self.n - 1),
28+
]
29+
),
30+
self.calculate_total_distance(
31+
[
32+
(0, self.stopover[1]),
33+
(self.stopover[1], self.stopover[0]),
34+
(self.stopover[0], self.n - 1),
35+
]
36+
),
37+
)
38+
)
39+
40+
def dijkstra(self, start: int, end: int) -> int:
41+
if start == end:
42+
return 0
43+
44+
heap, distances = [(0, start)], [-1] * (self.n + 1)
45+
46+
while heap:
47+
current_distance, current_vertex = heapq.heappop(heap)
48+
if current_vertex == end:
49+
return distances[end]
50+
51+
if distances[current_vertex] is not -1 and current_distance > distances[current_vertex]:
52+
continue
53+
54+
for next_vertex, distance in self.graph[current_vertex]:
55+
if distances[next_vertex] is -1 or current_distance + distance < distances[next_vertex]:
56+
distances[next_vertex] = current_distance + distance
57+
heapq.heappush(heap, (current_distance + distance, next_vertex))
58+
59+
return -1
60+
61+
def calculate_total_distance(self, paths: list[tuple[int, int]]) -> int:
62+
distances = [self.dijkstra(path[0], path[1]) for path in paths]
63+
64+
return -1 if any(distance == -1 for distance in distances) else sum(distances)
65+
66+
67+
if __name__ == "__main__":
68+
Problem().solve()

baekjoon/python/1504/sample.json

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

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