[Poj3260] the fewest coins multiple backpacks + full backpacks

Source: Internet
Author: User

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

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.