Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1446๋ฒˆ - ์ง€๋ฆ„๊ธธ (dp, ๋‹ค์ต์ŠคํŠธ๋ผ, ์ตœ๋‹จ๊ฒฝ๋กœ ๊ตฌํ•˜๊ธฐ)

๊ฐ์ž ๐Ÿฅ” 2022. 7. 22. 21:10
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1446

 

1446๋ฒˆ: ์ง€๋ฆ„๊ธธ

์ฒซ์งธ ์ค„์— ์ง€๋ฆ„๊ธธ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ณ ์†๋„๋กœ์˜ ๊ธธ์ด D๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 12 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ด๊ณ , D๋Š” 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ์ง€๋ฆ„๊ธธ์˜ ์‹œ์ž‘ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜, ์ง€๋ฆ„๊ธธ์˜ ๊ธธ์ด

www.acmicpc.net

 

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด ๋ฐฉ๋ฒ•

์ฒ˜์Œ์— ๋‚ด๊ฐ€ ์ ‘๊ทผํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€, 2์ฐจ์› dp ์˜€๋‹ค. ์ด๋ ‡๊ฒŒ ๋๊ฐ€์ง€ ๋ฐ€๊ณ  ๋‚˜๊ฐ”์–ด๋„ ํ’€๋ฆด๊ฑฐ ๊ฐ™๊ธดํ–ˆ๋‹ค. ๊ทผ๋ฐ, ์™œ ์ž๊พธ ์ƒ๊ฐ์ด ๋ง‰ํžˆ์ง€?

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ 2์ฐจ์› dp๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๊ณ  ํ–ˆ๋‹ค. ๊ทผ๋ฐ ๊ตณ์ด ์‹œ์ž‘์œ„์น˜๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์„ ๊ฑฐ๋Œ€ํ•˜๊ฒŒ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ํŒ๋‹จ๋˜์—ˆ๋‹ค. ์–ด์ฐจํ”ผ ๋„์ฐฉ์œ„์น˜์—์„œ shortcut์„ ๊ตฌํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—,, ๊ตณ์ด ์‹œ์ž‘์œ„์น˜๊นŒ์ง€ ๊ธฐ์–ตํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ? ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ํ’€์–ด์•ผ๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ํŒ๋‹จํ•œ๊ฒŒ ์ด๋ ‡๊ฒŒ! 

distance[์œ„์น˜] ์—๋‹ค๊ฐ€ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ตœ์†Œ๊ฑฐ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํŒ๋ณ„ํ•  ๊ฒƒ์ด๋ƒ? ๋งŒ์•ฝ shortcut์ด ์—†๋‹ค๋ฉด, ํ•ด๋‹น ์ธ๋ฑ์Šค ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ์ผ์น˜ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ผ๋‹จ distance ๋ฐฐ์—ด์—๋‹ค๊ฐ€ 1~D๊นŒ์ง€ ์ „๋ถ€ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€๋Š” distance์— ๊ทธ๋ƒฅ 1๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ๊ทผ๋ฐ ์œ„ ์˜ˆ์ œ1 ์—์„œ ์ฒ˜๋Ÿผ, ๋งŒ์•ฝ end point๊ฐ€ 50์ธ ๊ณณ์— shortcut์ด ์กด์žฌํ•œ๋‹ค๋ฉด, distance[50]์€ 50์ด ์•„๋‹ˆ๋ผ, 10์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋œ๋‹ค๋ฉด, distance[50]์— ์œ„์น˜ํ•œ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋Š” 
distance[49]+1 vs. distance[50]๊ฐ€ ๋œ๋‹ค!

๊ทธ๋Ÿฌ๋ฉด, ์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์€

  1. ๋จผ์ € distance์— 1๋ถ€ํ„ฐ d๊นŒ์ง€ ๋„ฃ์–ด์ฃผ๊ธฐ
  2. ๋„์ฐฉ์œ„์น˜๊ฐ€, shortcut์ด ์กด์žฌํ•˜๋Š” endpoint๋ผ๋ฉด shortcut๋„ฃ์–ด์ฃผ๊ธฐ
    • ์‹œ์ž‘์œ„์น˜๋กœ๋ถ€ํ„ฐ ๋น„๊ตํ•ด์•ผํ•จ. ๋‚ด๊ฐ€ ์ง€๊ธˆ 0์— ์žˆ์„ ๋•Œ, 0์ด startpoint์— ์กด์žฌํ•œ๋‹ค๋ฉด, endpoint์— shortcut์„ distance[startpoint] + shortcut์œผ๋กœ !! ํ•ด์•ผํ•จ!! ๊ทธ๋ž˜์•ผ shortcut์ด ๋”ํ•ด์ง„ ๊ฐ’์ด ๋‚˜์˜น

 

๐ŸŸ  ์†Œ์Šค์ฝ”๋“œ 

๋‚˜๋Š” ํ•˜๋‚˜์˜ ์„ธํŠธ (start, end, shortcut) ์„ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ start, end, shortcut ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค!

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1446_%EC%A7%80%EB%A6%84%EA%B8%B8/main.swift

let nd = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = nd[0]
let d = nd[1]
var start = [Int]()
var end = [Int]()
var shortcut = [Int]()
var distance = [Int]()
for i in 0..<d+1 {
    distance.append(i)
}
for _ in 0..<n {
    let temp = readLine()!.split(separator: " ").map { Int(String($0))! }
    start.append(temp[0])
    end.append(temp[1])
    shortcut.append(temp[2])
}

for j in 0..<d+1 {
    if j > 0 {
        distance[j] = min(distance[j-1]+1, distance[j])
    }
    for k in 0..<start.count {
        if j == start[k], end[k] <= d, distance[start[k]]+shortcut[k] < distance[end[k]] {
            distance[end[k]] = distance[start[k]]+shortcut[k]
        }
    }
}

print(distance[d])

 

๋ฐ˜์‘ํ˜•