Because W is too big. If DP[I][J]: To select the item I, the weight does not exceed the value of J items, the second cycle is too expensive to reconsider the recurrence relationship, Dp[i][j] means to select the I item, the value of the minimum weight J
1 intdp[ the][10005];2 3 intv[ the];4 intw[ the];5 6 intMain () {7 8 intn,w;9 while(SCANF ("%d", &n)! = EOF &&N) {Ten for(inti =0; I < n; i + +){ Onescanf"%d%d",&w[i],&v[i]); A } -scanf"%d",&W); - thememset (dp[0],0x7f7f7f7f,sizeof(dp[0])); -dp[0][0] =0; - /* - Dp[i][j] Indicates the minimum weight of the I item, the value of J + */ - for(inti =0; I < n; i + +){ + for(intj =0; J <= -* -; J + +){ A if(J <V[i]) { atdp[i+1][J] =Dp[i][j]; -}Else{ -dp[i+1][j] = min (Dp[i][j],dp[i][j-v[i]] +w[i]); - } - } - } in - intAns =0; to for(inti =0; I <=10000; i + +){ + if(Dp[n][i] <= W) ans =i; - } theprintf"%d\n", ans); * $ }Panax Notoginseng return 0; - } the /* + 4 A 2 3 the 1 2 + 3 4 - 5 $ $ 7 - */
0 1 Backpack Continuation, consider a recursive relationship