Advertisement
Sanlover

Untitled

May 20th, 2022
567
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 (size_t j = 0; j < roadAmount; j++)
  18.         {
  19.             if (j != i)
  20.             {
  21.                 tempSummary += roadConnections[i][j];
  22.             }
  23.         }
  24.         if (bestSummary == nullptr || tempSummary < *bestSummary)
  25.         {
  26.             bestSummary = new size_t(tempSummary);
  27.             bestIndex = i;
  28.         }
  29.     }
  30.     if (bestSummary == nullptr)
  31.     {
  32.         cout << "Something wrong" << endl;
  33.         return;
  34.     }
  35.  
  36.     cout << "The shortest vertex has index (0...N-1) = " << bestIndex << " with minimal road length to other vertexes ("
  37.         << *
  38.         bestSummary << ")";
  39. }
  40.  
  41. int main()
  42. {
  43.     const vector<vector<size_t>> roadConnections = {
  44.         {0, 8, 3, 4},
  45.         {8, 0, 3, 1},
  46.         {3, 3, 0, 5},
  47.         {4, 1, 5, 0}
  48.     };
  49.     task(roadConnections);
  50.     return 0;
  51. }
  52.  
Advertisement
RAW Paste Data Copied
Advertisement