Skip to content

Instantly share code, notes, and snippets.

@WhiteHyun
Last active August 21, 2023 13:40
Show Gist options
  • Save WhiteHyun/0600a83d8a463075e2f5b7d4557e748a to your computer and use it in GitHub Desktop.
Save WhiteHyun/0600a83d8a463075e2f5b7d4557e748a to your computer and use it in GitHub Desktop.
프로그래머스 - 합승 택시 요금 (Swift)
/// 합승 택시 요금 문제 풀이
struct Number72413 {
/// 합승 택시 요금 문제 풀이
/// - Parameters:
/// - n: 지점의 개수
/// - startNumber: 시작 정점
/// - a: A의 목표 정점
/// - b: B의 목표 정점
/// - fares: 예상 택시 요금 정보
/// - Returns: 예상 최저 택시 요금
func solution(_ n: Int, _ startNumber: Int, _ a: Int, _ b: Int, _ fares: [[Int]]) -> Int {
var graphMatrix: [[Int]] = Array(repeating: Array(repeating: 20_000_000, count: n), count: n)
for i in 0..<n {
graphMatrix[i][i] = 0
}
for fare in fares {
graphMatrix[fare[0] - 1][fare[1] - 1] = fare[2]
graphMatrix[fare[1] - 1][fare[0] - 1] = fare[2]
}
for throughNode in 0..<n {
for startNode in 0..<n {
for goalNode in 0..<n where graphMatrix[startNode][throughNode] + graphMatrix[throughNode][goalNode] < graphMatrix[startNode][goalNode] {
graphMatrix[startNode][goalNode] = graphMatrix[startNode][throughNode] + graphMatrix[throughNode][goalNode]
}
}
}
return (0..<n).map {
graphMatrix[$0][startNumber - 1] + graphMatrix[$0][a - 1] + graphMatrix[$0][b - 1]
}.min()!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment