-
Notifications
You must be signed in to change notification settings - Fork 0
플로이드 #134
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
플로이드 #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||
| let n = Int(readLine()!)! | ||||||||||||||||
| let m = Int(readLine()!)! | ||||||||||||||||
| var graph = Array(repeating:Array(repeating: Int.max, count: n + 1), count: n + 1) | ||||||||||||||||
|
|
||||||||||||||||
| for _ in 0..<m { | ||||||||||||||||
| let component = readLine()!.split(separator: " ").compactMap {Int($0)} | ||||||||||||||||
| let start = component[0] | ||||||||||||||||
| let end = component[1] | ||||||||||||||||
| let cost = component[2] | ||||||||||||||||
|
|
||||||||||||||||
| // 중복 있음;; 최솟값만 넣기 | ||||||||||||||||
| if graph[start][end] > cost { graph[start][end] = cost } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // for i in 1..<graph.count { | ||||||||||||||||
| // let row = graph[i].map { $0 == Int.max ? "-": String($0) }.joined(separator: " ") | ||||||||||||||||
| // print(row) | ||||||||||||||||
| // } | ||||||||||||||||
|
|
||||||||||||||||
| // 경유 | ||||||||||||||||
| for k in 1...n { | ||||||||||||||||
| // 시작 | ||||||||||||||||
| for i in 1...n { | ||||||||||||||||
| // 도착 | ||||||||||||||||
| for j in 1...n { | ||||||||||||||||
| if i == j { graph[i][j] = 0 } | ||||||||||||||||
| if graph[i][k] != Int.max && graph[k][j] != Int.max { | ||||||||||||||||
| let cost = graph[i][k] + graph[k][j] | ||||||||||||||||
| if graph[i][j] > cost { | ||||||||||||||||
| graph[i][j] = cost | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+25
to
+33
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. |
||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // 만약, i에서 j로 갈 수 없는 경우에는 그 자리에 0을 출력한다. | ||||||||||||||||
| for i in 1..<graph.count { | ||||||||||||||||
| graph[i].removeFirst() | ||||||||||||||||
| let row = graph[i].map { $0 == Int.max ? "0" : String($0) }.joined(separator: " ") | ||||||||||||||||
| print(row) | ||||||||||||||||
|
Comment on lines
+38
to
+41
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.
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
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.
그래프의 자기 자신으로 가는 경로(
i == j)의 비용은 항상 0입니다. 이를 3중 반복문 내부에서 매번 확인하는 대신, 초기화 시점에 미리 0으로 설정하는 것이 효율적입니다.