Skip to content

Commit a22e5b8

Browse files
committed
solved(python): baekjoon 28707
1 parent 8f035c3 commit a22e5b8

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

baekjoon/python/28707/__init__.py

Whitespace-only changes.

baekjoon/python/28707/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import heapq
2+
import sys
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n = int(read())
10+
self.array = tuple(map(int, read().split()))
11+
self.target = tuple(sorted(self.array))
12+
self.m = int(read())
13+
self.graph = {x: [] for x in range(self.n)}
14+
15+
for _ in range(self.m):
16+
left, right, weight = map(int, read().split())
17+
self.graph[left - 1].append((weight, right - 1))
18+
self.graph[right - 1].append((weight, left - 1))
19+
20+
def solve(self) -> None:
21+
print(self.dijkstra())
22+
23+
def dijkstra(self) -> int:
24+
heap, visited, costs = [(0, self.array)], set(), {self.array: 0}
25+
26+
while heap:
27+
cost, array = heapq.heappop(heap)
28+
if array == self.target:
29+
return cost
30+
31+
if array in visited:
32+
continue
33+
34+
visited.add(array)
35+
for src in range(self.n):
36+
for weight, dest in self.graph[src]:
37+
if src >= dest:
38+
continue
39+
40+
new_array = list(array)
41+
new_array[src], new_array[dest] = new_array[dest], new_array[src]
42+
new_array = tuple(new_array)
43+
44+
if new_array not in costs or cost + weight < costs[new_array]:
45+
costs[new_array] = cost + weight
46+
heapq.heappush(heap, (cost + weight, new_array))
47+
48+
return -1
49+
50+
51+
if __name__ == "__main__":
52+
Problem().solve()

baekjoon/python/28707/sample.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[
2+
{
3+
"input": [
4+
"4",
5+
"1 4 3 2",
6+
"4",
7+
"1 2 4",
8+
"2 3 3",
9+
"3 4 2",
10+
"1 4 10"
11+
],
12+
"expected": [
13+
"7"
14+
]
15+
},
16+
{
17+
"input": [
18+
"4",
19+
"1 3 1 3",
20+
"6",
21+
"1 2 3",
22+
"1 3 3",
23+
"1 4 3",
24+
"2 3 3",
25+
"2 4 1",
26+
"3 4 1"
27+
],
28+
"expected": [
29+
"2"
30+
]
31+
},
32+
{
33+
"input": [
34+
"5",
35+
"5 4 3 2 1",
36+
"2",
37+
"1 2 5",
38+
"3 4 3"
39+
],
40+
"expected": [
41+
"-1"
42+
]
43+
}
44+
]

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