Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 코딩테스트
- 티스토리챌린지
- API 활용 신청
- React
- apk 빌드
- 꿀팁 환영
- python
- 창업 300
- 훈수 가능
- 부산 맛집 OPEN API
- Dreamhack
- url 랜더링
- 오블완
- 드림핵
- 사업계획서
- 공공데이터 포털
- 개발
- expo
- 고고학 최고의 발견
- 새로고침
- redux state값 유지
- 보안
- 프로그래머스
- php-1
- Redux
- 블로그 뉴비
- level3
- web-view
- react-router-dom
Archives
- Today
- Total
1223v
[Programmers] Python 합승 택시 요금 (72413) 본문
https://school.programmers.co.kr/learn/courses/30/lessons/72413
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
다익스트라 응용 문제이다.
1. a, b 각각 가는 비용을 다익스트라로 구한다.
2. a, b 각각 구한 비용을 중심으로 비교할 대상을 반복을 돌며,
(s -> i 의 동행 거리) + (i -> a 거리) + (i -> b 거리) 가 최소가 되는 것을 찾으면 된다.
import heapq
def solution(n, s, a, b, fares):
answer = 0
graph=[[] for _ in range(n+1)]
for start, end, cost in fares:
graph[start].append((end,cost))
graph[end].append((start, cost))
def dijkstra(start):
hq = []
distance = [float('inf')] * (n+1)
distance[start] = 0
heapq.heappush(hq,(0,start))
while hq:
dist, now_node = heapq.heappop(hq)
for next_node, cost in graph[now_node]:
if distance[next_node] > dist+cost:
distance[next_node] = dist + cost
heapq.heappush(hq,(dist+cost, next_node))
return distance
origin_distance = dijkstra(s)
result = origin_distance[a] + origin_distance[b]
for i in range(n+1):
if i == s:
continue
else:
ab_distance=dijkstra(i)
result = min(result, origin_distance[i] + ab_distance[a] + ab_distance[b])
return result
print(solution(6,4,6,2, [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]]))
회고.
다익스트라를 부분적으로 잘라서 하는 문제를 풀어봤으나 이런식으로 나누는 것을 새로운 느낌이였다. 도움이 많이 된 문제였다.
728x90
'PS' 카테고리의 다른 글
[BOJ] Python 택배(1719) (0) | 2025.02.21 |
---|---|
[BOJ] Python 회문(17609) (0) | 2025.02.20 |
[BOJ] Python 플로이드(11404) (0) | 2025.02.18 |
[Programmers] Python 파괴되지 않은 건물 (92344) (0) | 2025.02.17 |
[BOJ] Python, Ruby 백준 최소 회의실 개수 (19598) (0) | 2025.02.17 |