다익 스트라 문제!!!!
문제 풀이
import Foundation
func solution(_ n:Int, _ s:Int, _ a:Int, _ b:Int, _ fares:[[Int]]) -> Int {
var matrix = Array(repeating: Array(repeating: Int(INT32_MAX), count: n), count: n)
(0..<n).forEach {
matrix[$0][$0] = 0
}
fares.forEach {
matrix[$0[0]-1][$0[1]-1] = $0[2]
matrix[$0[1]-1][$0[0]-1] = $0[2]
}
for k in 0..<n {
for i in 0..<n {
for j in 0..<n {
if matrix[i][k] + matrix[k][j] < matrix[i][j] {
matrix[i][j] = matrix[i][k] + matrix[k][j]
}
}
}
}
var answer = Int.max
(0..<n).forEach {
if answer > matrix[s-1][$0] + matrix[$0][a-1] + matrix[$0][b-1] {
answer = matrix[s-1][$0] + matrix[$0][a-1] + matrix[$0][b-1]
}
}
return answer
}
참고 : https://programmers.co.kr/learn/courses/30/lessons/72413