ps | |
---|---|
링크 | programmers.co.kr/… |
출처 | 프로그래머스 |
문제 번호 | 72413 |
문제명 | 합승 택시 요금 |
레벨 | Level 3 |
분류 |
그래프 |
시간복잡도 | O(n^2) |
인풋사이즈 | n<=200 |
사용한 언어 | Python |
해결날짜 | 2021/01/26 |
"""Solution code for "Programmers 72413. 합승 택시 요금".
- Problem link: https://programmers.co.kr/learn/courses/30/lessons/72413
- Solution link: http://www.teferi.net/ps/problems/programmers/72413
"""
from teflib import tgraph
def solution(n, s, a, b, fares):
wgraph = [{} for _ in range(n)]
for c, d, f in fares:
wgraph[c - 1][d - 1] = wgraph[d - 1][c - 1] = f
dists_s = tgraph.dijkstra(wgraph, s - 1)
dists_a = tgraph.dijkstra(wgraph, a - 1)
dists_b = tgraph.dijkstra(wgraph, b - 1)
return min(dists_s[x] + dists_a[x] + dists_b[x] for x in range(n))