Skip to content

Commit

Permalink
solve(programmers): LV3_합승 택시 요금_py
Browse files Browse the repository at this point in the history
# id: 문제 id를 숫자로 작성
# categories : 해당 문제의 유형을 ,로 구분하여 작성
# tags : 해당 문제의 태그를 ,로 구분하여 작성
# time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성
# try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성
# help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성
# url : 해당 문제의 url을 작성
id: 72413
categories: [탐색, 다익스트라, 플로이드]
tags: [2021 KAKAO BLIND RECRUITMENT
]
time: 120
try: 5
help: true
url: https://school.programmers.co.kr/learn/courses/30/lessons/72413#
  • Loading branch information
gogumaC committed Aug 8, 2024
1 parent 5d40086 commit 0b35087
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/programmers/level_1/그래프/LV3_합승_택시_요금.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import heapq
import sys

def dijkstra(n,graph,start):
pq=[(0,start)]
heapq.heapify(pq)
min_l=[sys.maxsize for _ in range(n+1)]
min_l[start]=0
visited=[False for _ in range(n+1)]

while pq:
(_,current)=heapq.heappop(pq)
if visited[current]:
continue
visited[current]=True
for nx,nc in graph[current]:
if visited[nx]:
continue
min_l[nx]=min(min_l[nx],min_l[current]+nc)
heapq.heappush(pq,(min_l[nx],nx))
#print(pq)
return min_l

def solution_with_dijkstra(n, s, a, b, fares):

graph={}
for st,fin,c in fares:
if st not in graph:
graph[st]=[]
if fin not in graph:
graph[fin]=[]
graph[st].append((fin,c))
graph[fin].append((st,c))

start_to=dijkstra(n,graph,s)
a_to=dijkstra(n,graph,a)
b_to=dijkstra(n,graph,b)


ans=sys.maxsize
for i in range(1,n+1):
total=start_to[i]+a_to[i]+b_to[i]
ans=min(ans,total)

return ans

def solution_with_floyd(n, s, a, b, fares):
import sys
min_l=[[sys.maxsize if i!=j else 0 for j in range(n+1)] for i in range(n+1)]
for st,fin,c in fares:
min_l[st][fin]=c
min_l[fin][st]=c

for via in range(1,n+1):
for start in range(1,n+1):
for finish in range(1,n+1):
if start==via or finish==via or start==finish:
continue
min_l[start][finish]=min(min_l[start][finish],min_l[start][via]+min_l[via][finish])
min_l[finish][start]=min_l[start][finish]
ans=min_l[s][a]+min_l[s][b]
for i in range(1,n+1):
together=min_l[s][i]
to_a=min_l[i][a]
to_b=min_l[i][b]
total=together+to_a+to_b
ans=min(ans,total)
return ans

0 comments on commit 0b35087

Please sign in to comment.