Files
hello-algo/ja/codes/csharp/chapter_dynamic_programming/unbounded_knapsack.cs
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

65 lines
2.3 KiB
C#

/**
* File: unbounded_knapsack.cs
* Created Time: 2023-07-12
* Author: hpstory ([email protected])
*/
namespace hello_algo.chapter_dynamic_programming;
public class unbounded_knapsack {
/* 個数制限なしナップサック問題:動的計画法 */
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.Length;
// dp テーブルを初期化する
int[,] dp = new int[n + 1, cap + 1];
// 状態遷移
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// ナップサックの容量を超える場合、品物 i を選ばない
dp[i, c] = dp[i - 1, c];
} else {
// 品物 i を選ばない場合と選ぶ場合の大きいほうの値
dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n, cap];
}
/* 個数制限なしナップサック問題:空間を最適化した動的計画法 */
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.Length;
// dp テーブルを初期化する
int[] dp = new int[cap + 1];
// 状態遷移
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// ナップサックの容量を超える場合、品物 i を選ばない
dp[c] = dp[c];
} else {
// 品物 i を選ばない場合と選ぶ場合の大きいほうの値
dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
[Test]
public void Test() {
int[] wgt = [1, 2, 3];
int[] val = [5, 11, 15];
int cap = 4;
// 動的計画法
int res = UnboundedKnapsackDP(wgt, val, cap);
Console.WriteLine("ナップサックの容量を超えない品物の最大価値は " + res);
// 空間を最適化した動的計画法
res = UnboundedKnapsackDPComp(wgt, val, cap);
Console.WriteLine("ナップサックの容量を超えない品物の最大価値は " + res);
}
}