A buys things from B. The value is m yuan, and there are n kinds of money. A has a certain amount of money, while B has an unlimited number.
The minimum number of banknotes can be used to meet the transaction requirements. For example, if a generates 50 + 25 and B finds 5, three bills are required.
A is transferred with multiple backpacks, and B is full with a backpack.
The O (n) algorithm is used to optimize multiple backpacks in this article. The binary conversion o (nlogn) is too lazy to write.
That can look at http://blog.csdn.net/vmurder/article/details/39472419
"
[Poj1014] multiple dividing backpacks, binary items split into 01 backpacks"
Code:
#include <cstdio>#include <cstring>#include <algorithm>#define N 150#define M 20000#define inf 0x3f3f3f3fusing namespace std;int n,m,ans=inf;int w[N],num[N];int f1[M],cnt[M],f2[M];int main(){//freopen("test.in","r",stdin);int i,j,k;scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d",&w[i]);for(i=1;i<=n;i++)scanf("%d",&num[i]);memset(f1,0x3f,sizeof(f1));memset(f2,0x3f,sizeof(f2));f1[0]=f2[0]=0;for(i=1;i<=n;i++){memset(cnt,0,sizeof(cnt));for(j=w[i];j<=15000;j++){if(f1[j]>f1[j-w[i]]+1&&cnt[j-w[i]]<num[i]){f1[j]=f1[j-w[i]]+1;cnt[j]=cnt[j-w[i]]+1;}f2[j]=min(f2[j],f2[j-w[i]]+1);}}for(i=m;i<=15000;i++){ans=min(ans,f1[i]+f2[i-m]);}if(ans>=15000)printf("-1\n");else printf("%d\n",ans);return 0;}
Copy the Translation results to Google
[Poj3260] the fewest coins multiple backpacks + full backpacks