-
Notifications
You must be signed in to change notification settings - Fork 0
최단경로 #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
최단경로 #131
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| let ve = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| let (v, e) = (ve[0], ve[1]) | ||
| let k = Int(readLine()!)! | ||
| var graph = [[(w: Int, v: Int)]](repeating: [], count: v + 1) | ||
|
|
||
| for _ in 0..<e { | ||
| let input = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| graph[input[0]].append((input[2], input[1])) | ||
| } | ||
|
|
||
| var dists = [Int](repeating: Int.max, count: v + 1) | ||
| var heap = Heap<(w: Int, v: Int)> { $0.w < $1.w } | ||
| heap.push((0, k)) | ||
| dists[k] = 0 | ||
|
|
||
| while let (curDist, curValue) = heap.pop() { | ||
| guard dists[curValue] == curDist else { continue } | ||
|
|
||
| for (nextWeight, nextValue) in graph[curValue] { | ||
| let nextDist = curDist + nextWeight | ||
| if nextDist < dists[nextValue] { | ||
| heap.push((nextDist, nextValue)) | ||
| dists[nextValue] = nextDist | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for i in 1...v { | ||
| let dist = dists[i] | ||
| print(dist == Int.max ? "INF" : dist) | ||
| } | ||
|
Comment on lines
+28
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| struct Heap<T> { | ||
| var elements: [T] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| let priority: (T, T) -> Bool | ||
|
|
||
| var isEmpty: Bool { elements.isEmpty } | ||
| var count: Int { elements.count } | ||
|
|
||
| init(_ priority: @escaping (T, T) -> Bool) { | ||
| self.elements = [] | ||
| self.priority = priority | ||
| } | ||
|
Comment on lines
+40
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| mutating func push(_ element: T) { | ||
| elements.append(element) | ||
| siftUp(from: elements.count - 1) | ||
| } | ||
|
|
||
| mutating func pop() -> T? { | ||
| guard !elements.isEmpty else { return nil } | ||
| elements.swapAt(0, elements.count - 1) | ||
| let removed = elements.removeLast() | ||
| siftDown(from: 0) | ||
| return removed | ||
| } | ||
|
|
||
| private mutating func siftUp(from index: Int) { | ||
| var newIndex = index | ||
| var parent = (index - 1) / 2 | ||
|
|
||
| while newIndex > 0 && priority(elements[newIndex], elements[parent]) { | ||
| elements.swapAt(newIndex, parent) | ||
| newIndex = parent | ||
| parent = (newIndex - 1) / 2 | ||
| } | ||
| } | ||
|
|
||
| private mutating func siftDown(from index: Int) { | ||
| var parent = index | ||
|
|
||
| while true { | ||
| let left = parent * 2 + 1 | ||
| let right = parent * 2 + 2 | ||
| var candidate = parent | ||
|
|
||
| if left < elements.count && priority(elements[left], elements[parent]) { | ||
| candidate = left | ||
| } | ||
| if right < elements.count && priority(elements[right], elements[candidate]) { | ||
| candidate = right | ||
| } | ||
|
|
||
| guard candidate != parent else { break } | ||
| elements.swapAt(candidate, parent) | ||
| parent = candidate | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readLine()과split을 이용한 입력 방식은 간선 수가 많은 경우(최대 300,000개) 시간 초과를 유발할 수 있습니다. 백준의 골드 등급 이상 문제에서는FileIO와 같은 더 빠른 입력 방식을 사용하는 것이 안전합니다.