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; }}