Skip to content

Commit 67033af

Browse files
committed
solved(python): baekjoon 1916
1 parent 3a72725 commit 67033af

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

baekjoon/python/1916/__init__.py

Whitespace-only changes.

baekjoon/python/1916/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import heapq
2+
import math
3+
import sys
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.m = int(read()), int(read())
12+
13+
self.graph = defaultdict(list[tuple[int, int]])
14+
for src, dest, price in [map(int, read().split()) for _ in range(self.m)]:
15+
self.graph[src].append((dest, price))
16+
17+
self.start, self.end = map(int, read().split())
18+
19+
def solve(self) -> None:
20+
print(self.dijkstra(self.start, self.end))
21+
22+
def dijkstra(self, start: int, end: int) -> int:
23+
heap, prices = [(0, start)], [0 if idx == start else math.inf for idx in range(self.n + 1)]
24+
25+
while heap:
26+
current_price, current_city = heapq.heappop(heap)
27+
if current_city == end:
28+
break
29+
30+
if current_price > prices[current_city]:
31+
continue
32+
33+
for next_city, price in self.graph[current_city]:
34+
if current_price + price < prices[next_city]:
35+
prices[next_city] = current_price + price
36+
heapq.heappush(heap, (current_price + price, next_city))
37+
38+
return int(prices[end])
39+
40+
41+
if __name__ == "__main__":
42+
Problem().solve()

baekjoon/python/1916/sample.json

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

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