[programmers] 합승 택시 요금 (python)

2022. 9. 2. 00:17·Study/algorithm
728x90
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/72413

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


문제 회고

다익스트라 알고리즘이 익숙지 않아서 개념 공부를 하고 푼 문제

다익스트라 알고리즘을 공부하고도 바로 풀지는 못했다. 문제를 작은 문제로 나눠서 풀었다. S와의 최단거리, A와의 최단 거리, B와의 최단 거리를 가지는 한 point를 찾아서 그때의 비용을 답으로 구한다. 총 세 가지 다익스트라 distance를 구했다. (S, A, B)


import heapq

def solution(n, s, a, b, fares):
    answer = 0
    INF = int(1e9)
    graph = [[] for _ in range(n + 1)]
    for point_a, point_b, fare in fares:
        graph[point_b].append((point_a, fare))
        graph[point_a].append((point_b, fare))
        
    def dijkstra(start):
        distance = [INF] * (n + 1)
        q = []
        heapq.heappush(q, (0, start))
        distance[start] = 0
        
        while q:
            dist, now = heapq.heappop(q)
            if distance[now] < dist:
                continue
            for i in graph[now]:
                cost = dist + i[1]
                if cost < distance[i[0]]:
                    distance[i[0]] = cost
                    heapq.heappush(q, (cost, i[0]))
        return distance
    
    a_distance = dijkstra(a)
    b_distance = dijkstra(b)
    s_distance = dijkstra(s)
    min_distance = INF
    for i in range(len(a_distance)):
        if a_distance != INF and b_distance != INF and s_distance != INF:
            min_distance = min(min_distance, a_distance[i] + b_distance[i] + s_distance[i])
    return min_distance
728x90
반응형

'Study > algorithm' 카테고리의 다른 글

[softeer] [21년 재직자 대회 예선] 회의실 예약 (python)  (0) 2022.09.12
[softeer] [21년 재직자 대회 예선] 이미지 프로세싱 (python)  (0) 2022.09.05
[softeer] [인증평가(1차) 기출] 로봇이 지나간 경로 (python)  (0) 2022.09.02
[softeer] [인증평가(1차) 기출] 안전운전을 도와줄 차세대 지능형 교통시스템 (python)  (3) 2022.09.01
[softeer] [인증평가(3차) 기출] 플레이페어 암호 (python)  (0) 2022.09.01
'Study/algorithm' 카테고리의 다른 글
  • [softeer] [21년 재직자 대회 예선] 회의실 예약 (python)
  • [softeer] [21년 재직자 대회 예선] 이미지 프로세싱 (python)
  • [softeer] [인증평가(1차) 기출] 로봇이 지나간 경로 (python)
  • [softeer] [인증평가(1차) 기출] 안전운전을 도와줄 차세대 지능형 교통시스템 (python)
성장형감자
성장형감자
공부 기록
    반응형
  • 성장형감자
    단순하게
    성장형감자
  • 전체
    오늘
    어제
    • Category (65)
      • Paper review (38)
        • 2D Object detection (11)
        • 3D Object detection (20)
        • 2D Segmentation (1)
        • 2D Classification (5)
        • 3D classification (1)
      • Programming (4)
        • Python (1)
        • Linux (3)
      • Project (0)
      • Study (23)
        • algorithm (20)
        • etc. (1)
        • Radar (2)
  • 인기 글

  • 블로그 메뉴

    • 홈
  • 250x250
  • hELLO· Designed By정상우.v4.10.0
성장형감자
[programmers] 합승 택시 요금 (python)
상단으로

티스토리툴바