Advertisement
Sanlover

Untitled

May 20th, 2022
534
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ppltasks.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. void task(const vector<vector<size_t>>& roadConnections)
  7. {
  8.     // We need to be sure that road matrix is equal by it's row sizes
  9.     const size_t roadAmount = roadConnections.size();
  10.  
  11.     size_t* bestSummary = nullptr;
  12.     size_t bestIndex = 0;
  13.  
  14.     for (size_t i = 0; i < roadAmount; i++)
  15.     {
  16.         size_t tempSummary = 0;
  17.         for (const auto& it : roadConnections[i])
  18.         {
  19.             tempSummary += it;
  20.         }
  21.         if (bestSummary == nullptr || tempSummary < *bestSummary)
  22.         {
  23.             bestSummary = new size_t(tempSummary);
  24.             bestIndex = i;
  25.         }
  26.     }
  27.     if (bestSummary == nullptr)
  28.     {
  29.         cout << "Something wrong" << endl;
  30.         return;
  31.     }
  32.  
  33.     cout << "The shortest vertex has index (0...N-1) = " << bestIndex << " with minimal road length to other vertexes ("
  34.         << *
  35.         bestSummary << ")";
  36. }
  37.  
  38. int main()
  39. {
  40.     const vector<vector<size_t>> roadConnections = {
  41.         {1, 2, 3, 4},
  42.         {4, 1, 3, 4},
  43.         {4, 1, 0, 4},
  44.         {2, 1, 5, 2}
  45.     };
  46.     task(roadConnections);
  47.     return 0;
  48. }
  49.  
Advertisement
RAW Paste Data Copied
Advertisement