알고리즘 문제 풀이

[Swift] BOJ 1238 파티

lgvv 2022. 5. 11. 18:35

BOJ 1238 파티

 

 

스위트프로 다익스트라 알고리즘 풀려고 하는데, 파이썬에 비해서 진짜 문법 때문에 너무 답답하다.

IDE 사용하면 틀린문법 바로 잡아주는데, 이걸 모두 외워서 치니까 .. 

 

특히 효율성을 따져야한다면 힙을 구현해서 사용해야 하는데, 평소에 만들어 둔 모듈 사용해서 쓰다가 직접 구현하자니 시간이 너무 많이 걸림

 

 

샘플 코드

//
//  b1238.swift
//  Algorithm
//
//  Created by Hamlit Jason on 2022/05/11.
//
// https://www.acmicpc.net/problem/1238
import Foundation

struct b1238 {
    static func run() {
        let input = readLine()!.split(separator: " ")
        var graph = [String: [String: Int]]()
        
        for _ in 0..<Int(input[1])! {
            let item = readLine()!.components(separatedBy:" ")
            let start = String(item[0])
            let end = String(item[1])
            let cost = Int(item[2])!
            
            if graph[item[0]] == nil {
                graph[start] = [end: cost]
            } else {
                var a = graph[start]!
                a[end] = cost
                graph[start] = a
            }
        }
        
//        print(graph)
        
        var timeList = [Int](repeating: 0, count: Int(input[0])! + 1)
        
        // 갈때
        for n in 1...Int(input[0])! {
            let answer = dijkstra(graph: graph, start: String(n))
            timeList[n] = answer[String(input[2])]!
        }
        
//        print(timeList)
        
        // 돌아올 때
        let answer = dijkstra(graph: graph, start: String(input[2]))
        answer.forEach {
            timeList[Int($0)!] += $1
        }
//        print(timeList)
        print(timeList.max()!)
        
        
    }
}