Skip to content

Commit 348f662

Browse files
committed
[2021kakao-p4] 합승 택시 요금
1 parent 0747c4e commit 348f662

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@
309309

310310
---
311311

312+
## 2021 KAKAO BLIND RECRUITMENT
313+
314+
| # || Problem | Note |
315+
| :-: | :-: | :------------------------------------------------------- | :--- |
316+
| 04 | | [합승 택시 요금](./src/_2021_KAKAO_BLIND_RECRUITMENT/P4) | |
317+
312318
## 2020 KAKAO BLIND RECRUITMENT
313319

314320
| # || Problem | Note |

Diff for: src/_2021_KAKAO_BLIND_RECRUITMENT/P4/Main.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package _2021_KAKAO_BLIND_RECRUITMENT.P4;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Solution sol = new Solution();
6+
System.out.println(sol.solution(6, 4, 6, 2,
7+
new int[][]{{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}}));
8+
System.out.println(sol.solution(7, 3, 4, 1,
9+
new int[][]{{5, 7, 9}, {4, 6, 4}, {3, 6, 1}, {3, 2, 3}, {2, 1, 6}}));
10+
System.out.println(sol.solution(6, 4, 5, 6,
11+
new int[][]{{2,6,6}, {6,3,7}, {4,6,7}, {6,5,11}, {2,5,12}, {5,3,20}, {2,4,8}, {4,3,9}}));
12+
}
13+
}
14+
15+
class Solution {
16+
17+
final static int INF = Integer.MAX_VALUE;
18+
static int[][] D;
19+
20+
public int solution(int n, int s, int a, int b, int[][] fares) {
21+
D = new int[n+1][n+1];
22+
for (int i = 1; i <= n; i++) {
23+
for (int j = 1; j <= n; j++) {
24+
if (i != j) D[i][j] = INF;
25+
}
26+
}
27+
28+
for (int[] fare : fares) {
29+
int c = fare[0], d = fare[1], f = fare[2];
30+
D[c][d] = f;
31+
D[d][c] = f;
32+
}
33+
34+
for (int k = 1; k <= n; k++) {
35+
for (int i = 1; i <= n; i++) {
36+
for (int j = 1; j <= n; j++) {
37+
if (D[i][k] == INF || D[k][j] == INF) continue;
38+
D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
39+
}
40+
}
41+
}
42+
43+
int ans = D[s][a] + D[s][b];
44+
for (int i = 1; i <= n; i++) {
45+
ans = Math.min(ans, D[s][i] + D[i][a] + D[i][b]);
46+
}
47+
48+
return ans;
49+
}
50+
}

Diff for: src/_2021_KAKAO_BLIND_RECRUITMENT/P4/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [2021 KAKAO BLIND RECRUITMENT - 4] 합승 택시 요금
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106773278-3d97b200-6684-11eb-983b-7ec540c30b44.png)
4+
5+
문제를 읽어보면 최단 경로 문제이며, 경유지 K를 탐색하며 n의 크기가 작으므로 플로이드-워셜 알고리즘 문제임을 알 수 있다.

0 commit comments

Comments
 (0)