标签:多重背包 cstring you center advance number cst sample ack
http://poj.org/problem?id=1787
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4512 | Accepted: 1425 |
Description
Input
Output
Sample Input
12 5 3 1 2 16 0 0 0 1 0 0 0 0 0
Sample Output
Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters. Charlie cannot buy coffee.
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define inf 0x3f3f3f3f 6 int coin[5]={0,1,5,10,25}; 7 int f[10005]; 8 int book[5]; 9 struct date 10 { 11 int num,type; 12 }Q[10005]; 13 int main() 14 { 15 int C[5],P,n,m,i,j,k; 16 while(cin>>P>>C[1]>>C[2]>>C[3]>>C[4]){ 17 if(!(P+C[1]+C[2]+C[3]+C[4])) break; 18 memset(f,-inf,sizeof(f)); 19 memset(Q,0,sizeof(Q)); 20 memset(book,0,sizeof(book)); 21 int W,L,l=0; 22 f[0]=0; 23 for(i=1;i<=4;++i) 24 { 25 l=0; 26 for(k=1;C[i];C[i]-=k,k*=2){ 27 if(C[i]<k) k=C[i]; 28 // cout<<i<<‘ ‘<<k<<endl; 29 W=k*coin[i]; 30 for(j=P;j>=W;--j) 31 { 32 33 34 if(f[j-W]!=-inf&&f[j]<f[j-W]+k) 35 { 36 f[j]=f[j-W]+k; 37 Q[j].num=k; 38 Q[j].type=i; 39 } 40 } 41 } 42 }//puts("dd"); 43 // cout<<f[P]<<endl; 44 if(f[P]<0) puts("Charlie cannot buy coffee."); 45 else{ 46 j=P; 47 i=0; 48 while(j){ 49 book[Q[j].type]+=Q[j].num; 50 j-=coin[Q[j].type]*Q[j].num; 51 } 52 printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",book[1],book[2],book[3],book[4]); 53 } 54 } 55 return 0; 56 }
标签:多重背包 cstring you center advance number cst sample ack
原文地址:http://www.cnblogs.com/zzqc/p/7774971.html