Skip to content

Commit 1018fb1

Browse files
committed
solved(python): baekjoon 11779
1 parent 59f40fd commit 1018fb1

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

baekjoon/python/11779/__init__.py

Whitespace-only changes.

baekjoon/python/11779/main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 = int(read())
12+
self.m = int(read())
13+
self.bus = defaultdict(list)
14+
15+
for _ in range(self.m):
16+
src, dest, weight = map(int, read().split())
17+
self.bus[src - 1].append((dest - 1, weight))
18+
19+
self.src, self.dest = map(lambda x: int(x) - 1, read().split())
20+
21+
def solve(self) -> None:
22+
weights = [math.inf] * self.n
23+
weights[self.src] = 0
24+
25+
queue, path = [(0.0, self.src)], [-1 for _ in range(self.n)]
26+
while queue:
27+
weight, city = heapq.heappop(queue)
28+
weight = -weight
29+
if weights[city] < weight:
30+
continue
31+
32+
for next_city, next_weight in self.bus[city]:
33+
if weight + next_weight < weights[next_city]:
34+
weights[next_city] = weight + next_weight
35+
path[next_city] = city
36+
heapq.heappush(queue, (-weights[next_city], next_city))
37+
38+
res = self.find_path(path)
39+
40+
print(int(weights[self.dest]))
41+
print(len(res))
42+
print(*res)
43+
44+
def find_path(self, path: list[int]) -> list[int]:
45+
current, result = self.dest, []
46+
while current != -1:
47+
result.append(current + 1)
48+
current = path[current]
49+
50+
return list(reversed(result))
51+
52+
53+
if __name__ == "__main__":
54+
Problem().solve()

baekjoon/python/11779/sample.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"3",
19+
"1 3 5"
20+
]
21+
},
22+
{
23+
"input": [
24+
"3",
25+
"2",
26+
"1 2 100000",
27+
"2 3 100000",
28+
"1 3"
29+
],
30+
"expected": [
31+
"200000",
32+
"3",
33+
"1 2 3"
34+
]
35+
}
36+
]

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