POJ 1837 Balance (01 backpack), poj1837

Source: Internet
Author: User

POJ 1837 Balance (01 backpack), poj1837

There is a balance. the right arm of the left arm is 15 in length and n, m is given. N indicates that there are several hooks. a negative number indicates the distance between the left arm and a positive number indicates the Right Arm. m indicates that there are m weights. You can find several ways to maintain the balance of the balance, all weights must be used up.

Idea: first, we need to clarify the role of the dp array. In dp [I] [j], I is the number of placed weights, j is the equilibrium state, and 0 is the equilibrium, j <0 left tilt, j> 0 right tilt, because j cannot be a negative number as the subscript, so we need to find a new balance point, because 15*20*20 = 7500, so the equilibrium point is set to 7500, and then we can obtain the dynamic equation dp [I] [j + w [I] * c [k]) + = dp [I-1] [j]

#include <iostream>#include <algorithm>#include <string.h>using namespace std;int dp[25][15005];int c[25],w[25];int main(){    int n ,m ,i ,j ,k;    while(cin>>n>>m){        for(i=1;i<=n;i++)            cin>>c[i];        for(i=1;i<=m;i++)            cin>>w[i];        memset(dp,0,sizeof(dp));        dp[0][7500]=1;        for(i=1;i<=m;i++){            for(j=0;j<=15000;j++)                if(dp[i-1][j])                    for(k=1;k<=n;k++)                        dp[i][j+w[i]*c[k]] += dp[i-1][j];        }        cout<<dp[m][7500]<<endl;    }}

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.