🖇️ 문제 링크
⌨️ 코드
import java.util.*;
class Solution {
final int INF = (int)1e8;
public int solution(int n, int s, int a, int b, int[][] fares) {
int[][] adj = new int[n + 1][n + 1];
for(int i = 1; i <= n; i++) {
Arrays.fill(adj[i], INF);
adj[i][i] = 0;
}
for(int[] fare : fares) {
int u = fare[0];
int v = fare[1];
int cost = fare[2];
adj[u][v] = cost;
adj[v][u] = cost;
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
if(adj[i][k] == INF) continue;
for(int j = 1; j <= n; j++)
adj[i][j] = Math.min(adj[i][j], adj[i][k] + adj[k][j]);
}
}
int ret = INF;
for(int i = 1; i <= n; i++)
ret = Math.min(ret, adj[s][i] + adj[i][a] + adj[i][b]);
return ret;
}
}
Uploaded by Notion2Tistory v1.1.0