Advertisement
Farjana_akter

Untitled

Aug 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int dp[100][100];
  5. int main()
  6. {
  7.  
  8. //freopen("in.txt","r",stdin);
  9. //freopen("out.txt","w",stdout);
  10. int weight[100],price[100],t,i,j,k,capacity,e,n;
  11. cin>>t;
  12. while(t--)
  13. {
  14. cin>>e;
  15. cin>>capacity;
  16. cin>>n;
  17. capacity-=e;
  18. for(i=1; i<=n; i++)
  19. {
  20. cin>>price[i];
  21. cin>>weight[i];
  22. }
  23. for(i=0; i<100; i++)
  24. {
  25. for(j=0; j<100; j++)
  26. dp[i][j]=100000000;
  27. }
  28.  
  29. dp[0][0]=0;
  30. for(i=1; i<=n; i++)
  31.  
  32. {
  33. for(j=0; j<=capacity; j++)
  34. {
  35. if(j<weight[i])
  36.  
  37. {
  38. dp[i][j]=dp[i-1][j];
  39. }
  40. else
  41. {
  42. dp[i][j]=min(dp[i][j-weight[i]]+price[i],dp[i][j]);
  43. }
  44. }
  45. }
  46. for(i=0;i<=n;i++)
  47. {
  48. for(j=0;j<=capacity;j++)
  49. cout<<dp[i][j]<<" ";
  50. cout<<endl;
  51. }
  52. if(dp[n][capacity]==100000000)
  53. cout<<"This is impossible."<<endl;
  54. else
  55. cout<<"The minimum amount of money in the piggy-bank is "<<dp[n][capacity]<<"."<<endl;
  56.  
  57. }
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement