Skip to content

Commit 9ceea09

Browse files
authored
Create 합승택시요금.py
1 parent 33d84ae commit 9ceea09

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

week14/gyuri/합승택시요금.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import heapq
2+
3+
def solution(n, s, a, b, fares):
4+
INF = int(1e9)
5+
graph = [[] for _ in range(n+1)]
6+
for x, y, z in fares:
7+
graph[x].append((z, y))
8+
graph[y].append((z, x))
9+
10+
def dijkstra(start):
11+
dist = [INF] * (n + 1)
12+
dist[start] = 0
13+
heap = []
14+
heapq.heappush(heap, (0, start))
15+
while heap:
16+
value, destination = heapq.heappop(heap)
17+
if dist[destination] < value:
18+
continue
19+
20+
for v, d in graph[destination]:
21+
next_value = value + v
22+
if dist[d] > next_value:
23+
dist[d] = next_value
24+
heapq.heappush(heap, (next_value, d))
25+
return dist
26+
27+
dp = [[]] + [dijkstra(i) for i in range(1, n+1)]
28+
# print(dp)
29+
answer = INF
30+
for i in range(1, n+1):
31+
answer = min(dp[i][a] + dp[i][b] + dp[i][s], answer)
32+
33+
return answer
34+
35+
'''
36+
두명이 같은 위치에서 택시를 타고 같이 내린다.
37+
그 후 각자 택시를 타 최소 요금으로 각자 목적지에 도착한다.
38+
39+
가중치가 있는 그래프 -> 다익스트라
40+
41+
'''

0 commit comments

Comments
 (0)