From 49132e0012f517f25be36e908b0978b9bc538198 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Thu, 30 May 2024 16:00:47 +0900 Subject: [PATCH] complete 1182.kt and 10971.kt --- zeta_kotlin/completed/10971.kt | 34 ++++++++++++++++++++++++++++++++++ zeta_kotlin/completed/1182.kt | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 zeta_kotlin/completed/10971.kt create mode 100644 zeta_kotlin/completed/1182.kt diff --git a/zeta_kotlin/completed/10971.kt b/zeta_kotlin/completed/10971.kt new file mode 100644 index 0000000..3bf7f93 --- /dev/null +++ b/zeta_kotlin/completed/10971.kt @@ -0,0 +1,34 @@ +import kotlin.math.min + +fun solve(N: Int, W: Array>): Int { + val D = ArrayDeque>>() // 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>(N) { // 0 to N-1 + readln().split(' ').map(String::toInt).toTypedArray() + } + + println(solve(N, W)) +} \ No newline at end of file diff --git a/zeta_kotlin/completed/1182.kt b/zeta_kotlin/completed/1182.kt new file mode 100644 index 0000000..aae76dd --- /dev/null +++ b/zeta_kotlin/completed/1182.kt @@ -0,0 +1,33 @@ +fun solve(N: Int, S: Int, I: List): Int { + val D = ArrayDeque>() + 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)) +} \ No newline at end of file