Multiple backpack minimum

The code can be used as a minimum knapsack problem (the default is case filled) template, using a two-dimensional array, personally feel good understanding, a two-dimensional array of one-dimensional arrays will understand the ok

Note that the initialization dp [0] [0] = 0; dp [0] [1 ~ n] = + ∞

Topic Link

Thinking: m is the capacity, for each i to the fourth volume of the article, since only four repeats of the same taken i, j up until the (current capacity) can be seen for the minimum full backpack. If you take the current number i, n ++, (ji fourth power) is to take the rest of the fourth power and digital

#include<stdio.h>
#include<string.h>
#include<math.h>
int dp[100][1000000];
#define min(a,b) a<b?a:b
int main(void){
    int n,i,k,y,j,a[1000],x,m;
scanf("%d",&m);x=sqrt(sqrt(m));

for(i=0;i<=x;i++)
{
    for(j=0;j<=m;j++)
    {
        if(i==0&&j!=0)
        {
            dp[i][j]=9999999;
        }
        else if(i==0&&j==0)
        {
            dp[i][j]=0;
        }
        else if(j<i*i*i*i)
        {
            dp[i][j]=dp[i-1][j];
        }
        else
        {
            dp[i][j]=min(dp[i-1][j],dp[i][j-i*i*i*i]+1);
        }
    }
}
printf("%d",dp[x][m]);

    return 0;
}

Published 11 original articles · won praise 0 · Views 130

Guess you like

Origin blog.csdn.net/weixin_45670020/article/details/104223629