hdu 题目1114 Piggy-Bank(完全背包)

た 入场券 2022-09-18 12:44 129阅读 0赞

http://acm.hdu.edu.cn/showproblem.php?pid=1114

完全背包

  1. /***************************
  2. # 2013-11-22 13:20:22
  3. # Time: MS Memory: K
  4. # Author: zyh
  5. ***************************/
  6. #include<iostream>
  7. #include<algorithm>
  8. #include<stdio.h>
  9. #include<string.h>
  10. #include<stdlib.h>
  11. #include<math.h>
  12. using namespace std;
  13. const int INF = 99999999;
  14. int n,f[10005],c[505],w[505];
  15. int completepack(int V)
  16. {
  17. int i,v;
  18. for(f[0]=0,i=1;i<=V;i++) f[i]=INF;
  19. for(i=0;i<n;i++){
  20. for(v=c[i];v<=V;v++){
  21. if(f[v]>f[v-c[i]] +w[i] )
  22. f[v] = f[v-c[i]]+w[i];
  23. }
  24. }
  25. return f[V];
  26. }
  27. int main()
  28. {
  29. int t,i,a,b,ans,flag;
  30. scanf("%d",&t);
  31. while(t--){
  32. scanf("%d%d",&a,&b);
  33. b = b - a;
  34. scanf("%d",&n);
  35. for(i=0;i<n;i++) scanf("%d%d",&w[i],&c[i]);
  36. ans = completepack(b);
  37. if(ans==INF) puts("This is impossible.");
  38. else printf("The minimum amount of money in the piggy-bank is %d.\n",ans);
  39. }
  40. return 0;
  41. }

发表评论

表情:
评论列表 (有 0 条评论,129人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1171(完全背包)

    题意:给一个数n,接下来n行,每行对应一个设备的两个值,第一个为设备的价值,第二个为该设备的数量。如何把所有设备一分为二,求两部分的价值总和尽量接近。   incl