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+ }
0 commit comments