1. Title Description: Click to open the link
2. Problem-Solving ideas: The topic is the classic 01 knapsack problems, I reverse enumeration of the writing is not introduced, mainly about the classic error. If the input n items are set to 0~n-1, the correct wording and the wrong wording are given.
3. Code:
(the correct wording)
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <algorithm># include<cassert> #include <string> #include <sstream> #include <set> #include <bitset># include<vector> #include <stack> #include <map> #include <queue> #include <deque># include<cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <ctime># Include<cctype> #include <functional>using namespace std; #define ME (s) memset (s,0,sizeof (s)) typedef long Long ll;typedef unsigned int uint;typedef unsigned long long ull;typedef pair <int, int> P; #define REP (i,n) for (int i=0;i< (n); i++) const int N=1000+10;int w[n],v[n];int Dp[n][n];int main () {int n,v; int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&v); for (int i=0;i<n;i++) scanf ("%d", &w[i]); for (int i=0;i<n;i++) scanf ("%d", &v[i]); Me (DP); Forint i=0;i<n;i++) for (int j=0;j<=v;j++) {dp[i+1][j]=dp[i][j]; if (J>=v[i]) Dp[i+1][j]=max (Dp[i+1][j],dp[i][j-v[i]]+w[i]); } printf ("%d\n", Dp[n][v]); }}
(Error one)
for (int i=0;i<n;i++) for (int j=0;j<=v;j++) { if (!i) dp[i][j]=0; else { dp[i][j]=dp[i-1][j]; if (J>=v[i]) Dp[i][j]=max (Dp[i][j],dp[i-1][j-v[i]]+w[i]); } } printf ("%d\n", Dp[n-1][v]);
(Error two)
for (int i=0;i<n;i++) for (int j=0;j<=v;j++) { if (!i) {dp[i][j]=0;continue;} DP[I][J]=DP[I-1][J]; if (J>=v[i]) Dp[i][j]=max (Dp[i][j],dp[i-1][j-v[i]]+w[i]); } printf ("%d\n", Dp[n-1][v]);
(Three wrong wording)
for (int i=0;i<n;i++) for (int j=0;j<=v;j++) { dp[i][j]= (i==0)? 0:dp[i-1][j]; if (J>=v[i]) Dp[i][j]=max (Dp[i][j],dp[i-1][j-v[i]]+w[i]); } printf ("%d\n", Dp[n-1][v]);
The above three kinds of wrong wording, if all understand why wrong, explain really understand the state transfer equation.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2602 Bone Collector