HDU 2602 Bone Collector

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.