Two-dimensional complete knapsack problem.
DP[I][J] Indicates the minimum cost when the x=i,y=j is made;
The
Dp[i][j]=min (dp[i][j],dp[i-wx[k]][j-wy[k]]+1);
As for the implementation of the equation can be modeled after one-dimensional knapsack problem write.
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath>using namespace Std;int dp[330] [330],wx[50],wy[50];int m,s;void input () {scanf ("%d%d", &m,&s); for (int i=0;i<m;i++) {scanf ("%d%d", &wx[i],&wy[i]); }}void solve () {memset (dp,0x3f,sizeof (DP)); dp[0][0]=0; for (int i=0;i<m;i++) for (int j=wx[i];j<=s;j++) for (int k=wy[i];k<=s;k++) {D P[j][k]=min (dp[j][k],dp[j-wx[i]][k-wy[i]]+1); } int ans=0x3f3f3f3f; for (int i=0;i<=s;i++) for (int j=0;j<=s;j++) {if (sqrt (i*i+j*j) ==s) ans=min (ANS,DP I [j]); } if (ans==0x3f3f3f3f) printf ("Not possible\n"); else printf ("%d\n", ans);} int main () {int t; scanf ("%d", &t); while (t--) {input (); Solve (); } return 0;}
UVA 10306--E-COINS+DP