youngjaeLee1026 2022. 4. 16. 17:08

1. 문제

스크란샷 2022-04-16 16 54 11

2. μž…μΆœλ ₯

스크란샷 2022-04-16 16 54 19

3. μž…μΆœλ ₯ μ˜ˆμ‹œ

스크란샷 2022-04-16 16 54 26

4. 문제 섀계

  • λ¬Έμ œμ—μ„œ μ–‘λ°©ν–₯ λ„λ‘œκ°€ μ•„λ‹˜μ— μ£Όμ˜ν•˜λΌλŠ” 점을 κ³ λ €ν•˜μ—¬, 단방ν–₯ κ·Έλž˜ν”„λ₯Ό κ΅¬μ„±ν•˜κ³  κ·Έ κ·Έλž˜ν”„μ— λŒ€ν•œ μ—­λ°©ν–₯ κ·Έλž˜ν”„λ₯Ό κ΅¬μ„±ν•œ ν›„
  • μ² μˆ˜λ„€ 집에 λŒ€ν•˜μ—¬ 두 κ·Έλž˜ν”„μ— λŒ€ν•΄ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ μš©ν•˜κ²Œ 되면, λͺ¨λ“  λ§ˆμ„ μ‚¬λžŒλ“€μ΄ μ² μˆ˜λ„€ 집에 였고 κ°€λŠ” μ΅œλ‹¨ 경둜λ₯Ό λͺ¨λ‘ ꡬ할 수 있음
  • λ”°λΌμ„œ 두 κ·Έλž˜ν”„μ— λŒ€ν•œ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ μš©ν•œ ν›„, 두 μ΅œλ‹¨ 거리 λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œμ˜ 합을 λ”ν•΄μ€ŒμœΌλ‘œμ¨ 문제λ₯Ό ν•΄κ²°ν•  수 있음

5. 전체 μ½”λ“œ

//MARK: - νŒŒν‹°

//MARK: - Framework
import Foundation

//MARK: - Type
struct Graph {
    //MARK: - Property
    var edges, costs: [Int]
    
    //MARK: - Initializer
    init() {
        self.edges = []
        self.costs = []
    }
}

//MARK: - Function
func dijkstra(_ graph: [Graph], _ N: Int, _ K: Int) -> Int {
    var visited: [Bool] = Array(repeating: false, count: N + 10)
    var distances: [Int] = Array(repeating: 987987987, count: N + 10)
    var sum: Int = 0
    distances[K] = 0
    
    for _ in stride(from: 1, through: N, by: 1) {
        var minCost, minIndex: Int
        minCost = 987987987
        minIndex = -1
        
        for j in stride(from: 1, through: N, by: 1) {
            if !visited[j] && minCost > distances[j] {
                minCost = distances[j]
                minIndex = j
            }
        }
        visited[minIndex] = true
        
        for j in 0..<graph[minIndex].edges.count {
            let y: Int = graph[minIndex].edges[j]
            let cost: Int = graph[minIndex].costs[j]
            
            if distances[y] > distances[minIndex] + cost {
                distances[y] = distances[minIndex] + cost
            }
        }
    }
    
    for i in stride(from: 1, through: N, by: 1) {
        sum += distances[i]
    }
    
    return sum
}

func solution() -> Void {
    //MARK: - Input
    guard let input: [String] = readLine()?.components(separatedBy: " ") else { return }
    let N: Int = input.map { Int($0) }[0] ?? 0
    let M: Int = input.map { Int($0) }[1] ?? 0
    let K: Int = input.map { Int($0) }[2] ?? 0
    var result: Int = 0
    var graphOne: [Graph] = Array(repeating: Graph(), count: N + 10)
    var graphTwo: [Graph] = Array(repeating: Graph(), count: N + 10)
    
    for _ in 0..<M {
        guard let inputData: [String] = readLine()?.components(separatedBy: " ") else { return }
        let a: Int = inputData.map { Int($0) }[0] ?? 0
        let b: Int = inputData.map { Int($0) }[1] ?? 0
        let c: Int = inputData.map { Int($0) }[2] ?? 0
        
        graphOne[a].edges.append(b)
        graphTwo[b].edges.append(a)
        graphOne[a].costs.append(c)
        graphTwo[b].costs.append(c)
    }
    
    //MARK: - Process
    result = dijkstra(graphOne, N, K) + dijkstra(graphTwo, N, K)
    
    //MARK: - Output
    print(result)
}
solution()

 

μ „μ²΄μ½”λ“œλŠ” μ—¬κΈ°μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.