complete 1182.kt and 10971.kt

This commit is contained in:
2024-05-30 16:00:47 +09:00
parent 5f87c9633a
commit 49132e0012
2 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import kotlin.math.min
fun solve(N: Int, W: Array<Array<Int>>): Int {
val D = ArrayDeque<Triple<Int, Int, Array<Int>>>() // now, cost, visited
D.addLast(Triple(0, 0, Array(N) { 0 }.also { it[0] = 1 }))
var min_cost = Int.MAX_VALUE
while (D.isNotEmpty()) {
val (now, cost, visited) = D.removeLast()
if (visited.all { it == 1 }) {
if (W[now][0] > 0) min_cost = min(min_cost, cost + W[now][0])
continue
} else {
for (i in 1 until N) {
if (visited[i] == 0 && W[now][i] > 0) {
D.addLast(Triple(i, W[now][i] + cost, visited.copyOf().also { it[i] = 1 }))
}
}
}
}
return min_cost
}
fun main() {
val N = readln().toInt()
val W = Array<Array<Int>>(N) { // 0 to N-1
readln().split(' ').map(String::toInt).toTypedArray()
}
println(solve(N, W))
}

View File

@@ -0,0 +1,33 @@
fun solve(N: Int, S: Int, I: List<Int>): Int {
val D = ArrayDeque<Pair<Int, Int>>()
D.addLast(Pair(0, 0))
var cnt = 0
while (D.isNotEmpty()) {
val (now, accum) = D.removeLast()
if (now == N) {
if (accum == S) {
cnt += 1
}
continue
} else {
D.addLast(Pair(now + 1, accum)) // 안 더하기
D.addLast(Pair(now + 1, accum + I[now]))
}
}
if (S == 0) {
cnt -= 1;
}
return cnt
}
fun main() {
val (N, S) = readln().split(' ').map(String::toInt)
val I = readln().split(' ').map(String::toInt)
println(solve(N, S, I))
}