Created
February 27, 2018 12:24
-
-
Save maehrm/b9df3d74ba3cc432e78eba5672970048 to your computer and use it in GitHub Desktop.
Knapsack Problem | Aizu Online Judge http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C&lang=jp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main(){ | |
int dp[101][10001]; | |
int value[101], weight[101]; | |
int n, w; | |
cin >> n >> w; | |
for (int i = 0; i < n; i++) | |
cin >> value[i] >> weight[i]; | |
for (int i = 0; i <= w; i++) | |
dp[0][i] = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j <= w; j++) { | |
if (j >= weight[i]) { | |
// int t = j/weight[i]; // ついついこんな風に考えてしまいます…。 | |
// dp[i+1][j] = max(dp[i][j-t*weight[i]]+t*value[i], dp[i][j]); | |
dp[i+1][j] = max(max(dp[i][j-weight[i]]+value[i], dp[i+1][j-weight[i]]+value[i]), | |
dp[i][j]); | |
} | |
else { | |
dp[i+1][j] = dp[i][j]; | |
} | |
} | |
} | |
cout << dp[n][w] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment