Advertisement
Guest User

fractionalknapsack

a guest
May 10th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int array[2][100], n, w, i, curwa, used[100], g[100], maxi = -1, totalprofit = 0;
  6. cin >> n; // номер объектов
  7. cin >> w; // вместимость рюкзака
  8. // массивы для сначала цены а потом веса
  9. for (i = 0; i < n; i++)
  10. {
  11. cin >> array[0][i] >> array[1][i];
  12. }
  13. for (i = 0; i < n; i++)
  14. {
  15. used[i] = 0;
  16. }
  17. for (i = 0; i <= n; i++)
  18. {
  19. g[i] = array[0][i]/array[1][i];
  20. } // подсчет стоимости
  21. //curw = w;
  22. //пока не наплним рюкзак
  23. for (curwa=w; curwa>=0; curwa -= array[1][maxi])
  24. {
  25. maxi = -1; // в оригинале -1
  26. //мак4симальный профит
  27. for (i = 0; i <= n; i++)
  28. {
  29. if ((used[i] == 0) && ((maxi == -1) || g[i]>g[maxi]))
  30. {
  31. maxi = i;
  32. }
  33. }
  34. used[maxi] = 1;
  35. //снижалку веса отправил в цикл
  36. // curwa -= array[1][maxi];
  37. //increase total profit
  38. totalprofit += array[0][maxi];
  39. }
  40. if (curwa < 0)
  41. // {
  42. //}
  43. //else
  44. {
  45. totalprofit -= array[0][maxi];
  46. totalprofit += g[maxi] * (array[1][maxi] + curwa);
  47. }
  48. //}
  49. //print total worth of objects filled in knapsack
  50. cout << totalprofit;
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement