Skip to content

Commit

Permalink
solve(BOJ): G3_1005_ACM_craft_kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gogumaC committed Jan 1, 2024
1 parent 5064a97 commit a68d678
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/solved/ac/class_5/G3_1005_ACM_Craft.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package solved.ac.class_5

import java.util.*

class BOJ1005() {
val sb = StringBuilder()
fun solve() {
val T = readln().toInt()
for (i in 0 until T) {
sb.append(findMinTime())
sb.append("\n")
}
println(sb)
}

fun findMinTime(): Int {

val (N, K) = readln().split(" ").map { it.toInt() }

val time = intArrayOf(0) + readln().split(" ").map { it.toInt() }

val forward = mutableMapOf<Int, MutableList<Int>>()
val reverse = mutableMapOf<Int, MutableList<Int>>()

val isStart = BooleanArray(N + 1) { true }

for (i in 1..N) {
forward[i] = mutableListOf()
reverse[i] = mutableListOf()
}
isStart[0] = false

for (i in 0 until K) {
val (start, next) = readln().split(" ").map { it.toInt() }
isStart[next] = false
forward[start]!!.add(next)
reverse[next]!!.add(start)
}

val q = LinkedList<Int>()
val res = IntArray(N + 1) { 0 }

for (i in 1..N) {
if (isStart[i]) {
q.add(i)
res[i] = time[i]
}
}

val W = readln().toInt()

while (q.isNotEmpty()) {
val current = q.poll()

if (current == W) return res[W]

forward[current]!!.forEach {
reverse[it]!!.remove(current)

if (res[it] < res[current] + time[it]) res[it] = res[current] + time[it]

if (reverse[it]!!.isEmpty()) {
q.add(it)
}
}
}

return 0
}
}

fun main() {
BOJ1005().solve()
}

0 comments on commit a68d678

Please sign in to comment.