Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[Swift] BOJ 1753 ์ตœ๋‹จ๊ฒฝ๋กœ ๋ณธ๋ฌธ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[Swift] BOJ 1753 ์ตœ๋‹จ๊ฒฝ๋กœ

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 4. 13. 13:42

BOJ 1753 ์ตœ๋‹จ๊ฒฝ๋กœ

 

โœ… ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ์˜€์Œ.

๊ทผ๋ฐ ๋‚˜ ๋ชปํ’ˆ ..ใ…Ž

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งž์•˜๋Š”๋ฐ ์ž๊พธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š”๊ฑฐ์ž„.

๊ทผ๋ฐ ๋‚˜์ค‘์— ์•Œ๊ณ ๋ณด๋‹ˆ๊นŒ ์ž๋ฃŒ๊ตฌ์กฐ ํž™์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์„œ๋”๋Ÿฌ๊ตฐ !_!

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•™์Šตํ•˜๊ธฐ๋กœ ํ•จ. ์—ฌ๊ธฐ์„œ ์‹œ๊ฐ„ ๋„ˆ๋ฌด ์žก์•„๋จน๊ธธ๋ž˜,, ์•„๋‹ˆ๋ฉด ํž™๋ถ€ํ„ฐ ํ•˜๊ณ  ๋‹ค์‹œ ๋Œ์•„์™€์•ผ์ง€.

 

โœ… ๋‚ด ์ฝ”๋“œ - ์‹œ๊ฐ„์ดˆ๊ณผ

struct b1753 {
    static func run() {
        // ์ž…๋ ฅ๋ถ€
        let firstLine = readLine()!.split(separator: " ").map( {Int(String($0))! })

        let secondLine = Int(readLine()!)!
        
        var table = [Int](repeating: Int(1e9), count: firstLine[0]+1)
        var graph = [Int: [Int: Int]]()
        
        for i in 1...firstLine[0] {
            graph[i] = [:]
        }
        
        for _ in 0..<firstLine[1] {
            let info = readLine()!.split(separator: " ").map { Int($0)! }
            
            let start = info[0]
            let end = info[1]
            let weight = info[2]
            
            var temp = graph[start]!
            
            if temp[end] == nil {
                temp[end] = weight
            } else {
                temp[end] = min(temp[end]!, weight)
            }
            graph[start] = temp
        }
        
        // ๊ตฌํ˜„๋ถ€ - ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
        
        var priorityQueue = [(Int, Int)]()
        
        var visited = [secondLine] // ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ
        table[secondLine] = 0
        
        graph[secondLine]!.forEach {
            
            priorityQueue.append(($0.key,$0.value))
        }
        
        priorityQueue = priorityQueue.sorted { $0.1 > $1.1 }
        
        var popNode = secondLine
        while !priorityQueue.isEmpty {
            
            // ํ…Œ์ด๋ธ” ์—…๋ฐ์ดํŠธ
            priorityQueue.forEach { (node, weight) in
                if !visited.contains(node) {
                    table[node] = min(table[node], weight + table[popNode])
                }
                
            }
            
            // ์šฐ์„ ์ˆœ์œ„ ํ ์—…๋ฐ์ดํŠธ -> ๋‹ค์Œ ์ž‘์—…์„ ์œ„ํ•จ.
            popNode = priorityQueue.last!.0
            visited.append(popNode)
            priorityQueue.removeAll()
            
            graph[popNode]?.forEach{
                priorityQueue.append(($0.key,$0.value))
            }
            priorityQueue = priorityQueue.sorted { $0.1 > $1.1 }
            
        }
        
        // ์ถœ๋ ฅ๋ถ€
        table[1...].forEach {
            if $0 == Int(1e9) {
                print("INF")
            } else {
                print($0)
            }
        }
    }
}

 

 

โœ… ์ •๋‹ต์ฝ”๋“œ - ์ถœ์ฒ˜๋Š” ์ œ์ผ ์•„๋ž˜์— ์žˆ์Šต๋‹ˆ๋‹ค.

import Foundation

struct PriorityQueue<T> {
  var array = [T]()
  let sort: (T, T) -> Bool

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

  var isEmpty: Bool {
    return array.isEmpty
  }

  var count: Int {
    return array.count
  }

  func peek() -> T? {
    return array.first
  }

  func leftChildIndex(ofParentAt index: Int) -> Int {
    return (2 * index) + 1
  }

  func rightChildIndex(ofParentAt index: Int) -> Int {
    return (2 * index) + 2
  }

  func parentIndex(ofChildAt index: Int) -> Int {
    return (index - 1) / 2
  }

  // MARK:- remove operation
  mutating func pop() -> T? {
    guard !isEmpty else {
      return nil
    }

    array.swapAt(0, count - 1)
    defer {
      siftDown(from: 0)
    }
    return array.removeLast()
  }

  mutating func siftDown(from index: Int) {
    var parent = index
    while true {
      let left = leftChildIndex(ofParentAt: parent)
      let right = rightChildIndex(ofParentAt: parent)
      var candidate = parent

      if left < count && sort(array[left], array[candidate]) {
        candidate = left
      }
      if right < count && sort(array[right], array[candidate]) {
        candidate = right
      }
      if candidate == parent {
        return
      }
      array.swapAt(parent, candidate)
      parent = candidate
    }
  }

  // MARK:- insert operation
  mutating func push(_ element: T) {
    array.append(element)
    siftUp(from: array.count - 1)
  }

  mutating func siftUp(from index: Int) {
    var child = index
    var parent = parentIndex(ofChildAt: child)
    while child > 0 && sort(array[child], array[parent]) {
      array.swapAt(child, parent)
      child = parent
      parent = parentIndex(ofChildAt: child)
    }
  }
}

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let k = Int(readLine()!)!
let v = input[0], e = input[1]
let INF = Int(1e9)

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

for _ in 0..<e {
  let i = readLine()!.split(separator: " ").map { Int(String($0))! }
  edges[i[0]].append((i[1], i[2]))
}

var dist = [Int](repeating: INF, count: v + 1)
var pq = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 })

dist[k] = 0
pq.push((k, 0))

while !pq.isEmpty {
  let (now, cost) = pq.pop()!

  if cost > dist[now] {
    continue
  }

  for (next, w) in edges[now] {
    if dist[now] + w <= dist[next] {
      dist[next] = dist[now] + w
      pq.push((next, dist[next]))
    }
  }
}

for i in 1...v {
  if dist[i] < INF {
    print(dist[i])
  } else {
    print("INF")
  }
}

 

 

 

 

 

(์ถœ์ฒ˜)

https://github.com/skyqnaqna/algorithm_study/blob/main/boj/Dijkstra/BOJ_1753.swift#L8

 

GitHub - skyqnaqna/algorithm_study: ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ์ฝ”๋“œ ๊ธฐ๋ก

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ์ฝ”๋“œ ๊ธฐ๋ก. Contribute to skyqnaqna/algorithm_study development by creating an account on GitHub.

github.com

 

Comments