목차

합승 택시 요금

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))