Skip to content

최단경로#131

Merged
github-actions[bot] merged 1 commit into
mainfrom
json
Apr 14, 2026
Merged

최단경로#131
github-actions[bot] merged 1 commit into
mainfrom
json

Conversation

@MinwooJe
Copy link
Copy Markdown
Member

🔗 문제 링크

✔️ 소요된 시간

50m + 20m

⏰ 시간 복잡도

O(ElogE)

@MinwooJe MinwooJe self-assigned this Apr 13, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements Dijkstra's algorithm to solve the shortest path problem, including a custom min-heap implementation. The review feedback suggests several performance optimizations for competitive programming, such as using faster I/O methods instead of readLine() and batching output into a single string to avoid multiple print calls. Additionally, minor refactoring of the Heap struct is recommended to simplify the initializer by declaring the elements array with an initial value.

var graph = [[(w: Int, v: Int)]](repeating: [], count: v + 1)

for _ in 0..<e {
let input = 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

readLine()split을 이용한 입력 방식은 간선 수가 많은 경우(최대 300,000개) 시간 초과를 유발할 수 있습니다. 백준의 골드 등급 이상 문제에서는 FileIO와 같은 더 빠른 입력 방식을 사용하는 것이 안전합니다.

Comment on lines +28 to +31
for i in 1...v {
let dist = dists[i]
print(dist == Int.max ? "INF" : dist)
}
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

반복문 내에서 print를 매번 호출하는 것은 출력 성능에 좋지 않습니다. 결과를 하나의 문자열로 모아서 한 번에 출력하는 것이 효율적입니다.

Suggested change
for i in 1...v {
let dist = dists[i]
print(dist == Int.max ? "INF" : dist)
}
print((1...v).map { i in
let dist = dists[i]
return dist == Int.max ? "INF" : "\(dist)"
}.joined(separator: "\n"))

}

struct Heap<T> {
var elements: [T]
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

elements 배열을 선언과 동시에 빈 배열로 초기화하여 코드의 의도를 명확히 하고 생성자를 간소화할 수 있습니다.

Suggested change
var elements: [T]
var elements: [T] = []

Comment on lines +40 to +43
init(_ priority: @escaping (T, T) -> Bool) {
self.elements = []
self.priority = priority
}
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

elements가 선언부에서 이미 초기화되어 있다면 생성자에서 중복으로 할당할 필요가 없습니다.

    init(_ priority: @escaping (T, T) -> Bool) {
        self.priority = priority
    }

@github-actions github-actions Bot merged commit bf02e45 into main Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant