Skip to content

Commit 00047bb

Browse files
committed
백준 1916번 최소비용 구하기
문제 링크: https://www.acmicpc.net/problem/1916
1 parent 35f8aaf commit 00047bb

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import heapq
2+
import sys
3+
from typing import Dict
4+
from collections import defaultdict
5+
6+
7+
def solve():
8+
N = int(sys.stdin.readline().strip())
9+
M = int(sys.stdin.readline().strip())
10+
maps = [sys.maxsize for _ in range(N + 1)]
11+
edges: Dict = defaultdict(list)
12+
13+
for _ in range(M):
14+
fr, to, cost = map(int, sys.stdin.readline().strip().split(' '))
15+
edges[fr].append([to, cost])
16+
17+
depature, destination = map(int, sys.stdin.readline().strip().split(' '))
18+
hq = []
19+
maps[depature] = 0
20+
heapq.heappush(hq, [0, depature])
21+
22+
while hq:
23+
cost_old, depature = heapq.heappop(hq)
24+
25+
if maps[depature] < cost_old:
26+
continue
27+
28+
for dest, cost_new in edges[depature]:
29+
if maps[dest] > cost_old + cost_new:
30+
maps[dest] = cost_old + cost_new
31+
heapq.heappush(hq, [cost_old + cost_new, dest])
32+
33+
print(maps[destination])
34+
35+
36+
if __name__ == '__main__':
37+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
5
2+
8
3+
1 2 2
4+
1 3 3
5+
1 4 1
6+
1 5 10
7+
2 4 2
8+
3 4 1
9+
3 5 1
10+
4 5 3
11+
1 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)