Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions WEEK11/백준_11404_플로이드/liv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 백준 - 11404 플로이드

let cityCount = Int(readLine()!)!
let busCount = Int(readLine()!)!
let basic = [Int](repeating: .max, count: cityCount+1)
var graph = [[Int]](repeating: basic, count: cityCount+1)

for _ in 1...busCount {
let line = readLine()!.split(separator: " ").compactMap { Int($0) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

입력 데이터의 양($M \le 100,000$)이 많을 때 readLine()split을 반복적으로 호출하는 것은 Swift에서 성능 저하의 주요 원인이 될 수 있습니다. 현재 문제의 시간 제한(2초) 내에는 통과할 가능성이 높지만, 데이터가 더 많거나 시간 제한이 촉박한 경우에는 FileIO와 같은 더 빠른 입력 방식을 사용하는 것이 권장됩니다.

graph[line[0]][line[1]] = min(graph[line[0]][line[1]], line[2])
}

for city in 1...cityCount {
graph[city][city] = 0
for start in 1...cityCount where graph[start][city] != Int.max {
for end in 1...cityCount where graph[city][end] != Int.max {
let newWeight = graph[start][city] + graph[city][end]
if graph[start][end] > newWeight { graph[start][end] = newWeight }
}
}
}

for i in 1...cityCount {
print((1...cityCount).map {
graph[i][$0] == Int.max ? "0" : String(graph[i][$0])
}.joined(separator: " "))
}