๐ ๋ฌธ์
๐ง ์์ด๋์ด
๊ทธ๋ํ์ ๊ฐ์ ๊ณผ ์ ์ ์ด ์ฃผ์ด์ง๊ณ ์์์ - ๋ชฉ์ ์ง - ๊ฐ์ ๋น์ฉ์ ์ ๋ ฅ๋ฐ๋ ์ ํ์ ์ธ ์ต์ ๋น์ฉ ๊ฒฝ๋ก ๋ฌธ์ .
๋จ ๊ฒฝ๋ก ๊ณผ์ ์ ๋ชจ๋ ํตํด์ผ ํ๋ฏ๋ก ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ์์์
ํน์ ์ง์ ์ ๋์ฐฉํ๊ธฐ ์ง์ ์ง์ ์ ๋ฐฐ์ด์ ๋ด์ ๋ณด๊ดํ๊ณ ์์์ ์ ๋๋ฌํ๊ธฐ ๊น์ง ๋ชฉ์ ์ง๋ก๋ถํฐ ๊ฑฐ๊พธ๋ก ์ถ๋ ฅํด๋ด๊ธฐ๋ง ํ๋ฉด ๋๊ฒ ๋ค.
๐ Kotlin Code
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.util.*
import kotlin.collections.ArrayList
fun dijkstra(startVertexNum: Int, distances: Array<Int>, preVertexes: Array<Int>, adjacentList: Array<ArrayList<IntArray>>, priorityQueue: PriorityQueue<IntArray>) {
distances[startVertexNum] = 0
// ์ฒซ์งธ ์ค์ ์ถ๋ฐ ๋์์์ ๋์ฐฉ ๋์๊น์ง ๊ฐ๋๋ฐ ๋๋ ์ต์ ๋น์ฉ์ ์ถ๋ ฅํ๋ค.
//๋์งธ ์ค์๋ ๊ทธ๋ฌํ ์ต์ ๋น์ฉ์ ๊ฐ๋ ๊ฒฝ๋ก์ ํฌํจ๋์ด์๋ ๋์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค. ์ถ๋ฐ ๋์์ ๋์ฐฉ ๋์๋ ํฌํจํ๋ค.
//์
์งธ ์ค์๋ ์ต์ ๋น์ฉ์ ๊ฐ๋ ๊ฒฝ๋ก๋ฅผ ๋ฐฉ๋ฌธํ๋ ๋์ ์์๋๋ก ์ถ๋ ฅํ๋ค.
priorityQueue.add(intArrayOf(startVertexNum, distances[startVertexNum]))
while (priorityQueue.isNotEmpty()) {
val (minVertexNum, totalWeight) = priorityQueue.poll()
// ๊ตฌ๋ฒ์ ์ ๋ณด์ธ ๊ฒฝ์ฐ skip
if (distances[minVertexNum] != totalWeight) continue
for (adjacentVertex in adjacentList[minVertexNum]) {
val (toVertexNum, toWeight) = adjacentVertex
// ๊ธฐ์กด ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ผ๋ฉด skip
if (distances[toVertexNum] <= totalWeight + toWeight) continue
distances[toVertexNum] = totalWeight + toWeight
priorityQueue.add(intArrayOf(toVertexNum, distances[toVertexNum]))
// ๋ชฉ์ ์ง ์ง์ ์ vertex ๋ฒํธ๋ก ์
๋ฐ์ดํธ
preVertexes[toVertexNum] = minVertexNum
}
}
}
fun main() {
val bufferedReader = BufferedReader(InputStreamReader(System.`in`))
val stringBuilder = StringBuilder()
val V = bufferedReader.readLine().toInt()
val E = bufferedReader.readLine().toInt()
// distance (index: toVertexNum, value: distance) ๋ฐฐ์ด ์ด๊ธฐํ
val distances = Array<Int>(V + 1){ Int.MAX_VALUE }
// index: toVertexNum, value: previous vertex num
val preVertexes = Array<Int>(V + 1){0}
// index: from vertex num, IntArray[0]: to vertex num, IntArray[1]: weight
val adjacentList = Array<ArrayList<IntArray>>(V + 1){ArrayList<IntArray>()}
repeat(E){
val (from, to, weight) = bufferedReader.readLine().split(" ").map{it.toInt()}
adjacentList[from].add(intArrayOf(to, weight))
}
val (startVertexNum, endVertexNum) = bufferedReader.readLine().split(" ").map{it.toInt()}
// weight min heap
val priorityQueue = PriorityQueue<IntArray>(E + 1){arr1, arr2-> arr1[1].compareTo(arr2[1])}
dijkstra(startVertexNum, distances, preVertexes, adjacentList, priorityQueue)
// distances, preVertexes ์
๋ฐ์ดํธ ์๋ฃ
stringBuilder.append("${distances[endVertexNum]}\n")
val paths = Stack<Int>()
var previousVertexNum = endVertexNum
paths.push(endVertexNum)
while(previousVertexNum != startVertexNum) {
previousVertexNum = preVertexes[previousVertexNum]
paths.push(previousVertexNum)
}
stringBuilder.append("${paths.size}\n")
while (paths.isNotEmpty()) {
val vertex = paths.pop()
stringBuilder.append("$vertex ")
}
println(stringBuilder.toString())
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2493 ํ (0) | 2022.10.03 |
---|---|
[๋ฐฑ์ค] 2217 ๋กํ (0) | 2022.09.28 |
[LeetCode] Same Tree (0) | 2022.09.05 |
[๋ฐฑ์ค 1158] ์์ธํธ์ค ๋ฌธ์ (2) | 2021.03.06 |
[Geeks for Geeks] Insert a node in a BST (0) | 2021.02.23 |