Test instructions: FJ body has a variety of coins, but to buy m-dollar things, want to buy with the fewest number of coins, and the amount of coins recovered is the least (the boss will be the least amount of automatic change), that is, the coin out and received the least number of coins.
Idea: The boss will automatically change the money, and in the least, the number of coins is not limited, then you can use a complete backpack to make up the minimum number of each number of coins. And FJ with the money is limited, then must use multiple backpack, because the money must be more than M, then we want to be more than the number of coins, but the FJ belt may be a lot of money, more than M many times may, then must have a backpack capacity limit, online said according to the drawer principle is M+max*max , Max here refers to the maximum face value. and give more money cap is Max*max, then the money back must also be max*max, so the full backpack part of the backpack capacity is max*max. The Max*max may be all right.
My idea: different from the above is the capacity of the multi-pack should be m+max, because if the need to recover more money than Max, then the boss is only a few more than the maximum denomination to you just. For example, buy a cigarette 1329 yuan, 13+1+1+4=19 Zhang Rmb, then we can give him 14, 15, 16, 17, 18 100, the boss will recover 71, 171, 271, 371, 471, you add Money, The boss also just take more 100 to give you, this is superfluous. Then no more than one 100 (maximum denomination), that is, 1329+100=1429 for the backpack capacity. It's been wrong many times!
1#include <iostream>2#include <stdio.h>3#include <cstring>4#include <algorithm>5 #defineINF 0x0ffffffa6 using namespacestd;7 Const intn= -;8 Const intlimit=30000;9 intN, t;Ten intCom[limit];//Full Backpack One intMul[limit];//Multiple Backpack A intBig; - structnode - { the intv,c; - }coin[n]; - - intcal () + { - for(intI=1; i<=big*big; i++) com[i]=INF; +com[0]=0; A for(intI=0; i<n; i++)//Full Backpack at { - for(intj=0; j+coin[i].v<=big*big; J + +)//Upper Big*big - { - if(com[j+coin[i].v]>com[j]+1) -com[j+coin[i].v]= com[j]+1 ; - } in - } to + intupto= t+ Big*big;//Multiple Backpack caps - for(intI=1; i<=upto; i++) mul[i]=INF; themul[0]=0; * for(intI=0; i<n; i++)//multiple Backpacks: 01 backpack + Binary $ {Panax Notoginseng intk=1, tmp=coin[i].c; - while(1) the { + if(k>tmp&&tmp) k=tmp; A Else if(k>tmp) Break; the for(intJ=upto; j>=k*coin[i].v; j-- ) + if(MUL[J-K*COIN[I].V]!=inf&&mul[j-k*coin[i].v]+k<Mul[j]) -mul[j]=mul[j-k*coin[i].v]+K; $tmp-=K; $k<<=1; - } - } the intANS=MUL[T];//just for T-dollar . - for(inti=t+1, j=1; i<upto; i++,j++ )Wuyi { the if(Com[j]==inf | | mul[i]==inf)Continue;//The INF's representation cannot just come together to filter out the price. - Else if(Ans>mul[i]+com[j]) ans= mul[i]+Com[j]; Wu } - About if(Ans==inf)return-1; $ returnans; - } - intMain () - { A //freopen ("Input.txt", "R", stdin); + while(cin>>n>>t) the { -big=-1; $ for(intI=0; i<n; i++) the { thescanf"%d",&coin[i].v); the if(BIG<COIN[I].V) big=coin[i].v; the } - for(intI=0; i<n; i++) scanf ("%d",&coin[i].c); inprintf"%d\n", Cal ()); the } the About return 0; the}
AC Code
POJ 3260 the fewest Coins minimum number of coins (full backpack + multiple backpack, mixed type)