Programming Algorithms-knapsack problem (three dynamic programming) code (C)

Source: Internet
Author: User

knapsack problem (three dynamic programming) code (C)


This address: Http://blog.csdn.net/caroline_wendy


Topic: http://blog.csdn.net/caroline_wendy/article/details/37912949


It can be solved by dynamic programming (dynamical programming, DP) , can be deduced by memory search , and can be solved in three different directions .

Dynamic planning is mainly the state transfer , need to understand clearly.


Code:

/* * main.cpp * * Created on:2014.7.17 * author:spike *//*eclipse CDT, gcc 4.8.1*/#include <stdio.h> #include <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm> Using namespace Std;class program {static const int max_n = 100;int n=4, w=5;int w[max_n] = {2,1,3,2}, v[max_n]={3,2,4,2}; int dp[max_n+1][max_n+1]; Default initialized to 0public:void solve () {for (int i=n-1; i>=0; i--) {for (int j=0; j<=w; ++j) {if (J<w[i]) {Dp[i][j] = dp[i+ 1][J];} else {Dp[i][j] = max (Dp[i+1][j], Dp[i+1][j-w[i]] + v[i]);}} printf ("result =%d\n", dp[0][w]);} void Solve1 () {for (int i=0, i<n; ++i) {for (int j=0; j<=w; ++j) {if (J<w[i]) {dp[i+1][j] = dp[i][j];} else {Dp[i +1][J] = max (Dp[i][j], dp[i][j-w[i]]+v[i]);}} printf ("result =%d\n", dp[n][w]);} void Solve2 () {for (Int. i=0; i<n; i++) {for (int j=0; j<=w; ++j) {dp[i+1][j] = max (Dp[i+1][j], dp[i][j]); if (j+w[i]& LT;=W) {Dp[i+1][j+w[i]] = max (Dp[i+1][j+w[i]], dp[i][j]+v[i]);}}} Printf ("result =%d\n", dp[n][w]);}}; int main (void) {program P;    P.solve2 (); return 0;}

Output:

result = 7


Save space and be able to use dynamic programming of 1-D arrays .

Code:

/* * main.cpp * *  Created on:2014.7.17 *      author:spike *//*eclipse CDT, gcc 4.8.1*/#include <stdio.h> #inclu De <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm >using namespace Std;class program {static const int max_n = 100;int n=4, w=5;int w[max_n] = {2,1,3,2}, v[max_n]={3,2,4 , 2};int dp[max_n+1];p ublic:void Solve () {memset (DP, 0, sizeof (DP)), for (Int. i=0; i<n; ++i) {for (int j=w; j>=w[i];- -J) {Dp[j] = max (Dp[j], dp[j-w[i]]+v[i]);}} printf ("result =%d\n", dp[w]);}; int main (void) {program P; P.solve ();    return 0;}

Output:

result = 7









Programming Algorithms-knapsack problem (three dynamic programming) code (C)

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.