2시간
package 백준.Platinum.P4.p10217_KCMTravel
import java.util.PriorityQueue
const val IMPOSSIBLE = Int.MAX_VALUE shr 2
fun main() {
repeat(readln().toInt()) {
val (N, M, K) = readln().trim().split(" ").map(String::toInt)
val roadLists = List(N + 1) { mutableListOf<Triple<Int, Int, Int>>() }
repeat(K) {
val (u, v, c, d) = readln().trim().split(" ").map(String::toInt)
roadLists[u].add(Triple(v, c, d))
}
roadLists.forEach { list -> list.sortWith(compareBy({ it.third }, { it.second })) }
val dp = Array(N + 1) { IntArray(M + 1) { IMPOSSIBLE } }.apply { this[1][0] = 0 }
val pq = PriorityQueue<Triple<Int, Int, Int>>(compareBy { it.third })
pq.add(Triple(1, 0, 0))
while (pq.isNotEmpty()) {
val (from, totalPrice, totalTime) = pq.poll()
if (dp[from][totalPrice] < totalTime) continue
for ((to, price, time) in roadLists[from]) {
val newTotalPrice = totalPrice + price
if (newTotalPrice > M) continue
val newTotalTime = totalTime + time
if (dp[to][newTotalPrice] > newTotalTime) {
for (nextPrice in newTotalPrice..M) {
if (dp[to][nextPrice] <= newTotalTime) break
dp[to][nextPrice] = newTotalTime
}
pq.add(Triple(to, newTotalPrice, newTotalTime))
}
}
}
println(dp[N].min().let { if (it == IMPOSSIBLE) "Poor KCM" else it })
}
}
https://www.acmicpc.net/problem/10217
굉장히 까다로운 문제였다. 쉽게 봤다가 시간 초과가 계속 나와서 시간을 많이 소요했다.