|
| 1 | +import Foundation |
| 2 | + |
| 3 | +struct Heap<T> { |
| 4 | + private var elements: [T] |
| 5 | + private let priority: (T, T) -> Bool |
| 6 | + |
| 7 | + init(sort: @escaping (T, T) -> Bool) { |
| 8 | + self.elements = [] |
| 9 | + self.priority = sort |
| 10 | + } |
| 11 | + |
| 12 | + var isEmpty: Bool { elements.isEmpty } |
| 13 | + |
| 14 | + mutating func insert(_ value: T) { |
| 15 | + elements.append(value) |
| 16 | + siftUp(from: elements.count - 1) |
| 17 | + } |
| 18 | + |
| 19 | + mutating func pop() -> T? { |
| 20 | + guard !elements.isEmpty else { return nil } |
| 21 | + elements.swapAt(0, elements.count - 1) |
| 22 | + let value = elements.removeLast() |
| 23 | + siftDown(from: 0) |
| 24 | + return value |
| 25 | + } |
| 26 | + |
| 27 | + private mutating func siftUp(from index: Int) { |
| 28 | + var child = index |
| 29 | + var parent = (child - 1) / 2 |
| 30 | + while child > 0 && priority(elements[child], elements[parent]) { |
| 31 | + elements.swapAt(child, parent) |
| 32 | + child = parent |
| 33 | + parent = (child - 1) / 2 |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private mutating func siftDown(from index: Int) { |
| 38 | + var parent = index |
| 39 | + while true { |
| 40 | + let left = 2 * parent + 1 |
| 41 | + let right = 2 * parent + 2 |
| 42 | + var candidate = parent |
| 43 | + |
| 44 | + if left < elements.count && priority(elements[left], elements[candidate]) { |
| 45 | + candidate = left |
| 46 | + } |
| 47 | + if right < elements.count && priority(elements[right], elements[candidate]) { |
| 48 | + candidate = right |
| 49 | + } |
| 50 | + if candidate == parent { break } |
| 51 | + elements.swapAt(parent, candidate) |
| 52 | + parent = candidate |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func dijkstra(start: Int, graph: [[(Int, Int)]], n: Int) -> [Int] { |
| 58 | + let INF = Int.max |
| 59 | + var dist = Array(repeating: INF, count: n + 1) |
| 60 | + var firstHop = Array(repeating: -1, count: n + 1) |
| 61 | + var pq = Heap<(Int, Int, Int)>(sort: { $0.0 < $1.0 }) |
| 62 | + |
| 63 | + dist[start] = 0 |
| 64 | + |
| 65 | + for (next, cost) in graph[start] { |
| 66 | + dist[next] = cost |
| 67 | + firstHop[next] = next |
| 68 | + pq.insert((cost, next, next)) |
| 69 | + } |
| 70 | + |
| 71 | + while !pq.isEmpty { |
| 72 | + let (curDist, cur, first) = pq.pop()! |
| 73 | + if curDist > dist[cur] { continue } |
| 74 | + |
| 75 | + for (next, cost) in graph[cur] { |
| 76 | + let newDist = curDist + cost |
| 77 | + if newDist < dist[next] { |
| 78 | + dist[next] = newDist |
| 79 | + firstHop[next] = firstHop[cur] == -1 ? next : firstHop[cur] |
| 80 | + pq.insert((newDist, next, firstHop[next])) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return firstHop |
| 86 | +} |
| 87 | + |
| 88 | +let nm = readLine()!.split(separator: " ").map { Int($0)! } |
| 89 | +let (n, m) = (nm[0], nm[1]) |
| 90 | +var graph = Array(repeating: [(Int, Int)](), count: n + 1) |
| 91 | + |
| 92 | +for _ in 0..<m { |
| 93 | + let line = readLine()!.split(separator: " ").map { Int($0)! } |
| 94 | + let (a, b, c) = (line[0], line[1], line[2]) |
| 95 | + graph[a].append((b, c)) |
| 96 | + graph[b].append((a, c)) |
| 97 | +} |
| 98 | + |
| 99 | +for i in 1...n { |
| 100 | + let firstHop = dijkstra(start: i, graph: graph, n: n) |
| 101 | + var row = "" |
| 102 | + for j in 1...n { |
| 103 | + if i == j { |
| 104 | + row += "- " |
| 105 | + } else { |
| 106 | + row += "\(firstHop[j]) " |
| 107 | + } |
| 108 | + } |
| 109 | + print(row.trimmingCharacters(in: .whitespaces)) |
| 110 | +} |
0 commit comments