Skip to content

Commit 8dee32f

Browse files
committed
solved(python): baekjoon 14938
1 parent 6923f3f commit 8dee32f

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

baekjoon/python/14938/__init__.py

Whitespace-only changes.

baekjoon/python/14938/main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.n, self.m, self.r = map(int, read().split())
9+
self.items = list(map(int, read().split()))
10+
self.roads = [
11+
[16 * int(row != col) for col in range(self.n)] for row in range(self.n)
12+
]
13+
14+
for _ in range(self.r):
15+
src, dest, distance = map(int, read().split())
16+
self.roads[src - 1][dest - 1] = distance
17+
self.roads[dest - 1][src - 1] = distance
18+
19+
def solve(self) -> None:
20+
path = self.find_path()
21+
22+
maximum = 0
23+
for src in range(self.n):
24+
count = 0
25+
for dest in range(self.n):
26+
if path[src][dest] <= self.m:
27+
count += self.items[dest]
28+
maximum = max(maximum, count)
29+
30+
print(maximum)
31+
32+
def find_path(self) -> list[list[int]]:
33+
path = self.roads[:]
34+
35+
for stopover in range(self.n):
36+
for src in range(self.n):
37+
for dest in range(self.n):
38+
path[src][dest] = min(
39+
path[src][dest],
40+
path[src][stopover] + path[stopover][dest],
41+
)
42+
43+
return path
44+
45+
46+
if __name__ == "__main__":
47+
Problem().solve()

baekjoon/python/14938/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 4",
5+
"5 7 8 2 3",
6+
"1 4 5",
7+
"5 2 4",
8+
"3 2 3",
9+
"1 2 3"
10+
],
11+
"expected": [
12+
"23"
13+
]
14+
}
15+
]

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