Advertisement
Sanlover

Untitled

May 18th, 2022
565
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct Milk
  6. {
  7.     double fillingTime;
  8.     double packingTime;
  9.  
  10.     Milk(const double& fillingTime,
  11.          const double& packingTime):
  12.         fillingTime(fillingTime),
  13.         packingTime(packingTime)
  14.     {
  15.     }
  16. };
  17.  
  18. size_t attempt = 1;
  19. using milk_t = Milk;
  20.  
  21. bool isCombined(const vector<size_t>& combo, const size_t& id)
  22. {
  23.     for (const auto& it : combo)
  24.     {
  25.         if (it == id)
  26.         {
  27.             return true;
  28.         }
  29.     }
  30.     return false;
  31. }
  32.  
  33.  
  34. void funcMethod(const vector<milk_t>& milks,
  35.                 vector<size_t>& currentCombo,
  36.                 vector<size_t>& bestCombo,
  37.                 double& currentTime,
  38.                 double*& bestTime,
  39.                 const double& packingTimeBefore
  40. )
  41. {
  42.     if (currentCombo.size() == milks.size())
  43.     {
  44.         if (bestTime == nullptr || currentTime <= *bestTime)
  45.         {
  46.             bestTime = new double(currentTime - packingTimeBefore);
  47.             bestCombo = currentCombo;
  48.             cout << endl << attempt++ << ": New best time" << endl;
  49.         }
  50.         else
  51.         {
  52.             cout << endl << attempt++ << ": Nothing interesting" << endl;
  53.         }
  54.         for (const auto& it : currentCombo)
  55.         {
  56.             cout << it << " ";
  57.         }
  58.         cout << endl << "CurrentTime: " << currentTime << " ; BestTime: " << *bestTime << endl;
  59.         return;
  60.     }
  61.  
  62.     for (size_t it = 0; it < milks.size(); it++)
  63.     {
  64.         if (!isCombined(currentCombo, it))
  65.         {
  66.             const milk_t tempMilk = milks[it];
  67.             double addition = 0;
  68.  
  69.             if (tempMilk.fillingTime > packingTimeBefore)
  70.             {
  71.                 addition = tempMilk.fillingTime - packingTimeBefore;
  72.             }
  73.  
  74.             addition += tempMilk.packingTime;
  75.             currentTime += addition;
  76.             currentCombo.push_back(it);
  77.             funcMethod(milks, currentCombo, bestCombo, currentTime, bestTime, tempMilk.packingTime);
  78.             currentCombo.pop_back();
  79.             currentTime -= addition;
  80.         }
  81.     }
  82. }
  83.  
  84. void func(const vector<milk_t>& milks)
  85. {
  86.     double currentTime = 0;
  87.     double* bestTime = nullptr;
  88.     vector<size_t> currentCombo, bestCombo;
  89.     funcMethod(milks, currentCombo, bestCombo, currentTime, bestTime, 0);
  90. }
  91.  
  92. int main()
  93. {
  94.     const vector<milk_t> milks = {
  95.         milk_t(5, 10),
  96.         milk_t(6, 4),
  97.         milk_t(8, 5),
  98.         milk_t(22, 15),
  99.         milk_t(3, 3)
  100.     };
  101.  
  102.     func(milks);
  103.     return 0;
  104. }
  105.  
Advertisement
RAW Paste Data Copied
Advertisement