kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: 0-1 Knapsack problem (Small weight)
(Mylib/Typical/knapsack_small_weight.cpp)

Operations

Requirements

Notes

Problems

References

Verified with

Code

#pragma once
#include <algorithm>
#include <vector>

namespace haar_lib {
  template <typename Weight, typename Value>
  Value knapsack_small_weight(int N, Weight cap, const std::vector<Weight> &w, const std::vector<Value> &v) {
    std::vector<std::vector<Value>> dp(N + 1, std::vector<Value>(cap + 1));

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= cap; ++j) {
        dp[i + 1][j] = std::max(dp[i + 1][j], dp[i][j]);
        if (j + w[i] <= cap) dp[i + 1][j + w[i]] = std::max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
      }
    }

    return dp[N][cap];
  }
}  // namespace haar_lib
#line 2 "Mylib/Typical/knapsack_small_weight.cpp"
#include <algorithm>
#include <vector>

namespace haar_lib {
  template <typename Weight, typename Value>
  Value knapsack_small_weight(int N, Weight cap, const std::vector<Weight> &w, const std::vector<Value> &v) {
    std::vector<std::vector<Value>> dp(N + 1, std::vector<Value>(cap + 1));

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= cap; ++j) {
        dp[i + 1][j] = std::max(dp[i + 1][j], dp[i][j]);
        if (j + w[i] <= cap) dp[i + 1][j + w[i]] = std::max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
      }
    }

    return dp[N][cap];
  }
}  // namespace haar_lib
Back to top page