Skip to content

Commit f8b4d6d

Browse files
committed
[level 3] Title: 합승 택시 요금, Time: 40.89 ms, Memory: 69.5 MB -BaekjoonHub
1 parent 1cf5e37 commit f8b4d6d

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# [level 3] 합승 택시 요금 - 72413
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/72413)
4+
5+
### 성능 요약
6+
7+
메모리: 69.5 MB, 시간: 40.89 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2021 KAKAO BLIND RECRUITMENT
12+
13+
### 채점결과
14+
15+
정확성: 50.0<br/>효율성: 50.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 07월 03일 23:31:26
20+
21+
### 문제 설명
22+
23+
<p><strong>[본 문제는 정확성과 효율성 테스트 각각 점수가 있는 문제입니다.]</strong></p>
24+
25+
<p>밤늦게 귀가할 때 안전을 위해 항상 택시를 이용하던 <code>무지</code>는 최근 야근이 잦아져 택시를 더 많이 이용하게 되어 택시비를 아낄 수 있는 방법을 고민하고 있습니다. "무지"는 자신이 택시를 이용할 때 동료인 <code>어피치</code> 역시 자신과 비슷한 방향으로 가는 택시를 종종 이용하는 것을 알게 되었습니다. "무지"는 "어피치"와 귀가 방향이 비슷하여 택시 합승을 적절히 이용하면 택시요금을 얼마나 아낄 수 있을 지 계산해 보고 "어피치"에게 합승을 제안해 보려고 합니다.</p>
26+
27+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/715ff493-d1a0-44d8-9273-a785280b3f1e/2021_kakao_taxi_01.png" title="" alt="2021_kakao_taxi_01.png"></p>
28+
29+
<p>위 예시 그림은 택시가 이동 가능한 반경에 있는 6개 지점 사이의 이동 가능한 택시노선과 예상요금을 보여주고 있습니다.<br>
30+
그림에서 <code>A</code>와 <code>B</code> 두 사람은 출발지점인 4번 지점에서 출발해서 택시를 타고 귀가하려고 합니다. <code>A</code>의 집은 6번 지점에 있으며 <code>B</code>의 집은 2번 지점에 있고 두 사람이 모두 귀가하는 데 소요되는 예상 최저 택시요금이 얼마인 지 계산하려고 합니다.</p>
31+
32+
<ul>
33+
<li>그림의 원은 지점을 나타내며 원 안의 숫자는 지점 번호를 나타냅니다.
34+
35+
<ul>
36+
<li>지점이 n개일 때, 지점 번호는 1부터 n까지 사용됩니다.</li>
37+
</ul></li>
38+
<li>지점 간에 택시가 이동할 수 있는 경로를 간선이라 하며, 간선에 표시된 숫자는 두 지점 사이의 예상 택시요금을 나타냅니다.
39+
40+
<ul>
41+
<li>간선은 편의 상 직선으로 표시되어 있습니다.</li>
42+
<li>위 그림 예시에서, 4번 지점에서 1번 지점으로(4→1) 가거나, 1번 지점에서 4번 지점으로(1→4) 갈 때 예상 택시요금은 <code>10</code>원으로 동일하며 이동 방향에 따라 달라지지 않습니다.</li>
43+
</ul></li>
44+
<li>예상되는 최저 택시요금은 다음과 같이 계산됩니다.
45+
46+
<ul>
47+
<li>4→1→5 : <code>A</code>, <code>B</code>가 합승하여 택시를 이용합니다. 예상 택시요금은 <code>10 + 24 = 34</code>원 입니다.</li>
48+
<li>5→6 : <code>A</code>가 혼자 택시를 이용합니다. 예상 택시요금은 <code>2</code>원 입니다.</li>
49+
<li>5→3→2 : <code>B</code>가 혼자 택시를 이용합니다. 예상 택시요금은 <code>24 + 22 = 46</code>원 입니다.</li>
50+
<li><code>A</code>, <code>B</code> 모두 귀가 완료까지 예상되는 최저 택시요금은 <code>34 + 2 + 46 = 82</code>원 입니다.</li>
51+
</ul></li>
52+
</ul>
53+
54+
<hr>
55+
56+
<h4><strong>[문제]</strong></h4>
57+
58+
<p>지점의 개수 n, 출발지점을 나타내는 s, <code>A</code>의 도착지점을 나타내는 a, <code>B</code>의 도착지점을 나타내는 b, 지점 사이의 예상 택시요금을 나타내는 fares가 매개변수로 주어집니다. 이때, <code>A</code>, <code>B</code> 두 사람이 s에서 출발해서 각각의 도착 지점까지 택시를 타고 간다고 가정할 때, 최저 예상 택시요금을 계산해서 return 하도록 solution 함수를 완성해 주세요.<br>
59+
만약, 아예 합승을 하지 않고 각자 이동하는 경우의 예상 택시요금이 더 낮다면, 합승을 하지 않아도 됩니다.</p>
60+
61+
<h4><strong>[제한사항]</strong></h4>
62+
63+
<ul>
64+
<li>지점갯수 n은 3 이상 200 이하인 자연수입니다.</li>
65+
<li>지점 s, a, b는 1 이상 n 이하인 자연수이며, 각기 서로 다른 값입니다.
66+
67+
<ul>
68+
<li>즉, 출발지점, <code>A</code>의 도착지점, <code>B</code>의 도착지점은 서로 겹치지 않습니다.</li>
69+
</ul></li>
70+
<li>fares는 2차원 정수 배열입니다.</li>
71+
<li>fares 배열의 크기는 2 이상 <code>n x (n-1) / 2</code> 이하입니다.
72+
73+
<ul>
74+
<li>예를들어, n = 6이라면 fares 배열의 크기는 2 이상 15 이하입니다. (<code>6 x 5 / 2 = 15</code>)</li>
75+
<li>fares 배열의 각 행은 [c, d, f] 형태입니다.</li>
76+
<li>c지점과 d지점 사이의 예상 택시요금이 <code>f</code>원이라는 뜻입니다.</li>
77+
<li>지점 c, d는 1 이상 n 이하인 자연수이며, 각기 서로 다른 값입니다.</li>
78+
<li>요금 f는 1 이상 100,000 이하인 자연수입니다.</li>
79+
<li>fares 배열에 두 지점 간 예상 택시요금은 1개만 주어집니다. 즉, [c, d, f]가 있다면 [d, c, f]는 주어지지 않습니다.</li>
80+
</ul></li>
81+
<li>출발지점 s에서 도착지점 a와 b로 가는 경로가 존재하는 경우만 입력으로 주어집니다.</li>
82+
</ul>
83+
84+
<hr>
85+
86+
<h5><strong>[입출력 예]</strong></h5>
87+
<table class="table">
88+
<thead><tr>
89+
<th>n</th>
90+
<th>s</th>
91+
<th>a</th>
92+
<th>b</th>
93+
<th>fares</th>
94+
<th>result</th>
95+
</tr>
96+
</thead>
97+
<tbody><tr>
98+
<td>6</td>
99+
<td>4</td>
100+
<td>6</td>
101+
<td>2</td>
102+
<td>[[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]]</td>
103+
<td>82</td>
104+
</tr>
105+
<tr>
106+
<td>7</td>
107+
<td>3</td>
108+
<td>4</td>
109+
<td>1</td>
110+
<td>[[5, 7, 9], [4, 6, 4], [3, 6, 1], [3, 2, 3], [2, 1, 6]]</td>
111+
<td>14</td>
112+
</tr>
113+
<tr>
114+
<td>6</td>
115+
<td>4</td>
116+
<td>5</td>
117+
<td>6</td>
118+
<td>[[2,6,6], [6,3,7], [4,6,7], [6,5,11], [2,5,12], [5,3,20], [2,4,8], [4,3,9]]</td>
119+
<td>18</td>
120+
</tr>
121+
</tbody>
122+
</table>
123+
<h5><strong>입출력 예에 대한 설명</strong></h5>
124+
125+
<hr>
126+
127+
<p><strong>입출력 예 #1</strong><br>
128+
문제 예시와 같습니다.</p>
129+
130+
<p><strong>입출력 예 #2</strong><br>
131+
<img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/934fcb5a-f844-4b02-b7fa-46198123be05/2021_kakao_taxi_02.png" title="" alt="2021_kakao_taxi_02.png"></p>
132+
133+
<ul>
134+
<li>합승을 하지 않고, <code>B</code>는 <code>3→2→1</code>, <code>A</code>는 <code>3→6→4</code> 경로로 각자 택시를 타고 가는 것이 최저 예상 택시요금입니다.</li>
135+
<li>따라서 최저 예상 택시요금은 <code>(3 + 6) + (1 + 4) = 14</code>원 입니다.</li>
136+
</ul>
137+
138+
<p><strong>입출력 예 #3</strong><br>
139+
<img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/179cc8ad-73d2-46c9-95e9-2363f3cb345d/2021_kakao_taxi_03.png" title="" alt="2021_kakao_taxi_03.png"></p>
140+
141+
<ul>
142+
<li><code>A</code>와 <code>B</code>가 <code>4→6</code> 구간을 합승하고 <code>B</code>가 6번 지점에서 내린 후, <code>A가</code>6→5` 구간을 혼자 타고 가는 것이 최저 예상 택시요금입니다.</li>
143+
<li>따라서 최저 예상 택시요금은 <code>7 + 11 = 18</code>원 입니다.</li>
144+
</ul>
145+
146+
147+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
static class Edge implements Comparable<Edge> {
5+
int node, cost;
6+
public Edge(int node, int cost) {
7+
this.node = node;
8+
this.cost = cost;
9+
}
10+
11+
@Override
12+
public int compareTo(Edge o) {
13+
return Integer.compare(this.cost, o.cost);
14+
}
15+
}
16+
17+
public static int[] dj(Map<Integer, List<Edge>> graph, int n, int start) {
18+
int INF = Integer.MAX_VALUE;
19+
int[] distance = new int[n+1];
20+
Arrays.fill(distance, INF);
21+
22+
Queue<Edge> pq = new PriorityQueue<>();
23+
pq.add(new Edge(start, 0));
24+
distance[start] = 0;
25+
26+
while(!pq.isEmpty()) {
27+
Edge current = pq.poll();
28+
if (distance[current.node] < current.cost) continue;
29+
30+
List<Edge> neighbors = graph.get(current.node);
31+
if (neighbors == null) continue;
32+
33+
for (Edge next : neighbors) {
34+
int nextCost = current.cost + next.cost;
35+
if (distance[next.node] > nextCost) {
36+
distance[next.node] = nextCost;
37+
pq.add(new Edge(next.node, nextCost));
38+
}
39+
}
40+
}
41+
return distance;
42+
}
43+
44+
public int solution(int n, int s, int a, int b, int[][] fares) {
45+
// 먼저 그래프를 만들고 초기화
46+
Map<Integer, List<Edge>> graph = new HashMap<>();
47+
for (int[] fare: fares) {
48+
int u = fare[0], v = fare[1], w = fare[2];
49+
graph.computeIfAbsent(u, x -> new ArrayList<>()).add(new Edge(v, w));
50+
graph.computeIfAbsent(v, x -> new ArrayList<>()).add(new Edge(u, w));
51+
}
52+
53+
// 각 점에 대해서 다익스트라 진행
54+
int[] dist_s = dj(graph, n, s);
55+
int[] dist_a = dj(graph, n, a);
56+
int[] dist_b = dj(graph, n, b);
57+
58+
int answer = Integer.MAX_VALUE;
59+
// for(int i = 1부터 n까지) 만들어놓은 dist_a[i] != Integer.MAX_VALUE 비교하기
60+
for (int i = 1; i <= n; i++) {
61+
if (dist_s[i] != Integer.MAX_VALUE && dist_a[i] != Integer.MAX_VALUE && dist_b[i] != Integer.MAX_VALUE) {
62+
answer = Math.min(answer, dist_s[i] + dist_a[i] + dist_b[i]);
63+
}
64+
}
65+
return answer;
66+
}
67+
}

0 commit comments

Comments
 (0)