DPL_1_C Knapsack Problem
Table of contents
# Problem
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C
# Time complexity
# Solution
#define MAX_N 101
#define MAX_V 1001
#define MAX_CAP 10001
struct Item { Int value, weight; };
Int N, CAP;
vector<Item> items(MAX_N, {-1, -1});
vector<Int> dp(MAX_CAP, 0);
Int v, w;
void input() {
cin >> N >> CAP;
loop(n,0,N) {
cin >> v >> w;
items[n] = { v, w };
}
}
void solve() {
loop(cap,1,CAP+1) {
loop(n,0,N) {
if (items[n].weight <= cap)
dp[cap] = max(dp[cap-items[n].weight] + items[n].value, dp[cap]);
}
}
cout << dp[CAP] << endl;
}
int main(void) {
input();
solve();
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!