문제 소개 programmers.co.kr/learn/courses/30/lessons/72413

내용

지점의 개수 n, 출발지점을 나타내는 s, A의 도착지점을 나타내는 a, B의 도착지점을 나타내는 b, 지점 사이의 예상 택시요금을 나타내는 fares가 매개변수로 주어집니다. 이때, A, B 두 사람이 s에서 출발해서 각각의 도착 지점까지 택시를 타고 간다고 가정할 때, 최저 예상 택시요금을 계산해서 return 하도록 solution 함수를 완성해 주세요.
만약, 아예 합승을 하지 않고 각자 이동하는 경우의 예상 택시요금이 더 낮다면, 합승을 하지 않아도 됩니다.

입력

  • 지점갯수 n은 3 이상 200 이하인 자연수입니다.
  • 지점 s, a, b는 1 이상 n 이하인 자연수이며, 각기 서로 다른 값입니다.
    • 즉, 출발지점, A의 도착지점, B의 도착지점은 서로 겹치지 않습니다.
  • fares는 2차원 정수 배열입니다.
  • fares 배열의 크기는 2 이상 n x (n-1) / 2 이하입니다.
    • 예를들어, n = 6이라면 fares 배열의 크기는 2 이상 15 이하입니다. (6 x 5 / 2 = 15)
    • fares 배열의 각 행은 [c, d, f] 형태입니다.
    • c지점과 d지점 사이의 예상 택시요금이 f원이라는 뜻입니다.
    • 지점 c, d는 1 이상 n 이하인 자연수이며, 각기 서로 다른 값입니다.
    • 요금 f는 1 이상 100,000 이하인 자연수입니다.
    • fares 배열에 두 지점 간 예상 택시요금은 1개만 주어집니다. 즉, [c, d, f]가 있다면 [d, c, f]는 주어지지 않습니다.
  • 출발지점 s에서 도착지점 a와 b로 가는 경로가 존재하는 경우만 입력으로 주어집니다.

출력

가장 저렴하게 합승해서 도착지 까지의 비용을 리턴하시오.

해결 방법

아이디어

n의 200이므로 n^3인 플루이드 와샬 알고리즘도 시간내에 동작한다.

 

1. 모든 점에서 최단 거리를 구한다 (플루이드 와샬)

2. 구한최단거리로 출발지에서 한점까지의 비용 + 각 A, B까지의 비용을 계산

3. 최단 비용을 반환한다.

실제 풀이 코드

public class Solution {
    public int solution(int n, int s, int a, int b, int[][] fares) {
        int answer = 0;

        List<List<Node>> adjList = new ArrayList<>();

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

        for (int[] fare : fares) {
            adjList.get(fare[0] - 1).add(new Node(fare[1] - 1, fare[2]));
            adjList.get(fare[1] - 1).add(new Node(fare[0] - 1, fare[2]));
        }

        int[][] dist = floydWarshal(n, adjList);

        answer = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if (dist[s - 1][i] == Integer.MAX_VALUE
                    || dist[i][a - 1] == Integer.MAX_VALUE
                    || dist[i][b - 1] == Integer.MAX_VALUE) continue;
            int subTotal = dist[s - 1][i];
            subTotal += dist[i][a - 1];
            subTotal += dist[i][b - 1];

            answer = Math.min(answer, subTotal);
        }
        return answer;
    }

    private int[][] floydWarshal(int n, List<List<Node>> adjList) {
        int[][] dist = new int[n][n];

        for (int i = 0; i < n; i++) {
            Arrays.fill(dist[i], Integer.MAX_VALUE);
            dist[i][i] = 0;
        }

        for (int i = 0; i < adjList.size(); i++) {
            for (Node node : adjList.get(i)) {
                dist[i][node.from] = node.value;
            }
        }

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (dist[i][k] == Integer.MAX_VALUE || dist[k][j] == Integer.MAX_VALUE) continue;
                    if (dist[i][j] > dist[i][k] + dist[k][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }

        return dist;
    }

    static class Node {
        int from;
        int value;

        public Node(int from, int value) {
            this.from = from;
            this.value = value;
        }
    }

    @Test
    public void test1() {
        int n = 6;
        int s = 4;
        int a = 6;
        int b = 2;

        int[][] fares = {{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}};

        Assert.assertEquals(82, solution(n, s, a, b, fares));
    }
}

결과

 

BELATED ARTICLES

more