프로그래머스 3단계 : 합승 택시 요금 (Java 자바)

2023. 9. 14. 22:52·💻 코딩테스트/programmers
728x90

 

import java.util.*;

class Edge implements Comparable<Edge>{
    int index;
    int cost;
    Edge(int index, int cost){
        this.index = index;
        this.cost = cost;
    }
    @Override
    public int compareTo(Edge e){
        return this.cost - e.cost;
    }
}

class Solution {

    static final int MAX = 20000001; // 200 * 100000 + 1
    static ArrayList<ArrayList<Edge>> graph;

    public int solution(int n, int s, int a, int b, int[][] fares) {
        int answer = Integer.MAX_VALUE;

        graph = new ArrayList<>();
        for(int i = 0 ; i <= n ; i ++){
            graph.add(new ArrayList<>());
        }

        for(int[] i : fares){
            graph.get(i[0]).add(new Edge(i[1], i[2]));
            graph.get(i[1]).add(new Edge(i[0], i[2]));
        }

        int[] startA = new int[n + 1];
        int[] startB = new int[n + 1];
        int[] start = new int[n + 1];

        Arrays.fill(startA, MAX);
        Arrays.fill(startB, MAX);
        Arrays.fill(start, MAX);

        startA = dijkstra(a, startA);
        startB = dijkstra(b, startB);
        start = dijkstra(s, start);

        for(int i = 1; i <= n ; i ++) answer = Math.min(answer, startA[i] + startB[i] + start[i]);
        return answer;
    }

    static int[] dijkstra (int start, int[] costs) {
        PriorityQueue<Edge> pq = new PriorityQueue<>();
        pq.offer(new Edge(start, 0));
        costs[start] = 0;

        while (!pq.isEmpty()) {
            Edge now = pq.poll();
            int nIndex = now.index;
            int nCost = now.cost;

            if(nCost > costs[nIndex]) continue;

            ArrayList<Edge> edges = graph.get(nIndex);
            for (Edge edge : edges) {
                int cost = costs[nIndex] + edge.cost;

                if (cost < costs[edge.index]) {
                    costs[edge.index] = cost;
                    pq.offer(new Edge(edge.index, cost));
                }
            }
        }
        return costs;
    }
}
728x90
저작자표시 비영리 변경금지 (새창열림)

'💻 코딩테스트 > programmers' 카테고리의 다른 글

프로그래머스 3단계 : 1차 셔틀버스 (Java 자바)  (1) 2023.09.14
프로그래머스 3단계 : 자물쇠와 열쇠 (Java 자바)  (0) 2023.09.14
프로그래머스 3단계 : 경주로 건설 (Java 자바)  (0) 2023.09.14
프로그래머스 3단계 : 다단계 칫솔 (Java 자바)  (0) 2023.09.13
프로그래머스 3단계 : 길 찾기 게임 (Java 자바)  (0) 2023.09.13
'💻 코딩테스트/programmers' 카테고리의 다른 글
  • 프로그래머스 3단계 : 1차 셔틀버스 (Java 자바)
  • 프로그래머스 3단계 : 자물쇠와 열쇠 (Java 자바)
  • 프로그래머스 3단계 : 경주로 건설 (Java 자바)
  • 프로그래머스 3단계 : 다단계 칫솔 (Java 자바)
gxxg
gxxg
함께 일하고 싶은 개발자를 꿈꾸는 예비개발자의 공부 기록
  • gxxg
    공공
    gxxg
  • 전체
    오늘
    어제
    • 분류 전체보기 (138)
      • ☁️ 구름 x 카카오 Deep Dive 풀스택 (7)
        • html, css (1)
        • Java (3)
        • 스프링 MVC (0)
      • 💻 코딩테스트 (89)
        • 백준 (2)
        • programmers (87)
      • SQLD (1)
      • Language (3)
        • Java (2)
        • JavaScript (1)
      • Style Sheet (0)
        • CSS (0)
        • SCSS & SASS (0)
      • DBMS (2)
        • Oracle (2)
        • MySQL (0)
        • postgresql (0)
        • 데이터베이스 기초 이론 (0)
      • React (0)
      • SpringBoot (0)
      • JSP (2)
      • 알고리즘 (0)
      • 2023-02 몰입형 SW 정규 교육 (24)
        • 9월 프로젝트 (8)
      • 벽돌깨기 (4)
      • Etc (4)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    오블완
    프로젝트 구조
    자바
    Lv0
    spring
    자바스크립트
    구현체
    eclipse
    springboot
    0단계
    CSS
    programmers
    이클립스
    Lv2
    톰캣
    DFS
    JSP
    java
    회원 관리 시스템
    POST
    junit 테스트
    LV3
    2단계
    티스토리챌린지
    javascript
    3단계
    코테
    HTML
    코딩테스트
    프로그래머스
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
gxxg
프로그래머스 3단계 : 합승 택시 요금 (Java 자바)
상단으로

티스토리툴바