Greedy Algorithm (Knapsack Problem)

Source: Internet
Author: User

The package can bear 15kg of the weight. The quality of five objects is 12, 2, 1, 4, and 1. The price is, and 1. the maximum value of the package is

Problem Analysis:

1. first obtain the bid value = GRID/weight and save it in an array;

2. Sort elements in a logarithm group in ascending order of values.

3. From the beginning of high-value installation, the problem of the backpack can be divided into the problem of cutting and not cutting backpack Problems

 

  1. // Question about the detachable backpack
  2. # Include <iostream>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int P, W;
  8. Double V;
  9. };
  10. Int main ()
  11. {
  12. A f [5], T;
  13. Int I, j, S, M, N, sum;
  14. Cin> N;
  15. For (I = 0; I <5; I ++)
  16. {
  17. Cin> F [I]. P> F [I]. W;
  18. F [I]. V = (double) f [I]. P/(double) f [I]. W;
  19. }
  20. For (I = 0; I <5; I ++)
  21. For (j = 0; j <5; j ++)
  22. {
  23. If (F [I]. V> F [J]. V)
  24. {
  25. T = f [I];
  26. F [I] = f [J];
  27. F [J] = T;
  28. }
  29. }
  30. S = 0;
  31. M = 0;
  32. Sum = 0;
  33. For (I = 0; I <5; I ++)
  34. {
  35. S = S + F [I]. W;
  36. If (n> S)
  37. {
  38. M = n-s;
  39. Sum = sum + F [I]. P;
  40. }
  41. Else
  42. Sum = sum + F [I]. V * m;
  43. }
  44. Cout <sum <Endl;
  45. Return 0;
  46. }

 
  1. Unpartitioned backpack
  2. # Include <iostream>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int P, W;
  8. Double V;
  9. };
  10. Int main ()
  11. {
  12. A f [5], T;
  13. Int I, j, S, M, N;
  14. Cin> N;
  15. For (I = 0; I <5; I ++)
  16. {
  17. Cin> F [I]. P> F [I]. W;
  18. F [I]. V = (double) f [I]. P/(double) f [I]. W;
  19. }
  20. For (I = 0; I <5; I ++)
  21. For (j = 0; j <5; j ++)
  22. {
  23. If (F [I]. V> F [J]. V)
  24. {
  25. T = f [I];
  26. F [I] = f [J];
  27. F [J] = T;
  28. }
  29. }
  30. S = 0;
  31. M = 0;
  32. For (I = 0; I <5; I ++)
  33. {
  34. S = S + F [I]. W;
  35. If (S> N) break;
  36. M = m + F [I]. P;
  37. }
  38. Cout <m <Endl;
  39. Return 0;
  40. }

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.