[0-1 backpack TEMPLATE] poj 3624

Source: Internet
Author: User

First look at an unoptimized two-dimensional space DP:

#include <iostream>#include <cstdio>#include <cmath>#include <memory.h>using namespace std;const int maxn1=3500;const int maxn2=1300;int dp[maxn2][maxn2];//int dp[maxn2];int w[maxn1],v[maxn2];int m,n;int max(int x,int y){ return x>y?x:y;}int main(){    freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) {  for(int i=1;i<=n;i++)   cin >> w[i] >> v[i]; } memset(dp,0,sizeof(dp)); int j; for(int i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            if(j>=w[i])            dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);            else            dp[i][j] = dp[i-1][j];        }    } cout << dp[n][m] << endl; return 0;}

 

 

 

Improvement:

1. Two-Dimensional Optimization to one-dimensional

2. Write-down

#include <iostream>#include <cstdio>#include <cmath>#include <memory.h>using namespace std;const int maxn1=3500;const int maxn2=13000;int dp[maxn2],w[maxn1],v[maxn2];int m,n;int max(int a, int b){    if(a>b) return a ;    else return b ;}int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)) {        memset(dp,0,sizeof(dp));  for(int i=1;i<=n;i++)   cin >> w[i] >> v[i];        int j;        for(int i=1;i<=n;i++)        {            for(j=m;j>=w[i];j--)            {                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);                cout << j << " j "<< dp[j]<< endl;            }            cout <<endl;        }        cout << dp[m] << endl; } return 0;}

Give a group of data for ease of understanding:

6 J 4
5 J 4
4 J 4
3 J 4
2 J 4
1 J 4

6 J 10
5 J 10
4 J 10
3 J 10
2 J 6

6 J 22
5 J 18
4 J 16
3 J 12

6 J 23
5 J 19
4 J 16
3 J 12
2 J 7

23

 

 

 

 

Finally, the template is provided:

#include <iostream>#include <cstdio>#include <cmath>#include <memory.h>using namespace std;const int maxn1=3500;const int maxn2=13000;int dp[maxn2],w[maxn1],v[maxn1];int m,n;int max(int a, int b){    if(a>b) return a ;    else return b ;}int main(){    //freopen("in.txt","r",stdin);    while(~scanf("%d%d",&n,&m))    {        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)            cin >> w[i] >> v[i];        int j;        for(int i=1;i<=n;i++)            for(j=m;j>=w[i];j--)                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);        cout << dp[m] << endl;    }    return 0;}

 

[0-1 backpack TEMPLATE] poj 3624

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.