Advertisement
allia

краскал

Jan 7th, 2021 (edited)
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int INF = 10000;
  5.  
  6. struct rebro
  7. {
  8.  int begin, end, ves;
  9.  void new_rebro(int begin, int end, int ves)
  10.  {
  11.    this->begin = begin;
  12.    this->end = end;
  13.    this->ves = ves;
  14.  }
  15. };
  16.  
  17. class Graph
  18. {
  19.   private:
  20.   int n, shet_reber, dlina = 0;
  21.   int *color, **path;
  22.   rebro *arr;
  23.  
  24.   public:
  25.   Graph (rebro *matrix, int x, int shet)
  26.   {
  27.     n = x;
  28.     shet_reber = shet;
  29.     arr = matrix;
  30.     color = new int[n];
  31.  
  32.     for (int i = 0; i < n; i++)
  33.      color[i] = i;
  34.  
  35.     path = new int*[n];
  36.  
  37.     for(int i = 0; i < n; i++)
  38.      path[i] = new int[n];
  39.  
  40.     for(int i = 0; i < n; i++)
  41.      for (int j = 0; j < n; j++)
  42.       path[i][j] = 0;
  43.   }
  44.  
  45.   void sort_reber(int first, int last);
  46.   void algorithm_kras();
  47.   void get_result_kras();
  48. };
  49.  
  50. void Graph::sort_reber(int first, int last)
  51. {
  52. int x, i, j, c = first, d = last;
  53.  
  54. while (c < d)
  55. {
  56.   int m = (c+d)/2;
  57.  
  58.  if (arr[c].ves > arr[d].ves && arr[c].ves < arr[m].ves)
  59.  x = arr[c].ves;
  60.   else if (arr[d].ves > arr[c].ves && arr[d].ves < arr[m].ves)
  61.    x = arr[d].ves;
  62.     else x = arr[m].ves;
  63.  
  64.  i = c;
  65.  j = d;
  66.    
  67.    while (i < j)
  68.    {
  69.     while (arr[i].ves < x)
  70.      i++;
  71.     while (arr[j].ves > x)
  72.      j--;
  73.  
  74.     if (i <= j)
  75.      {
  76.        swap(arr[i], arr[j]);
  77.        i++;
  78.        j--;
  79.      }
  80.    }
  81.  
  82. if (j-c < d-i)
  83.  {
  84.    if (c < j)
  85.     sort_reber(c, j);
  86.     c = i;
  87.  }
  88. else
  89.  {
  90.    if (i<d)
  91.   sort_reber(i, d);
  92.    d = j;
  93.  }
  94. }
  95. }
  96.  
  97. void Graph::algorithm_kras()
  98. {
  99.  for(int i = 0; i < shet_reber; i++)
  100.   {
  101.     if (color[arr[i].begin] != color[arr[i].end])
  102.      {
  103.        path[arr[i].begin][arr[i].end] = arr[i].ves;
  104.        path[arr[i].end][arr[i].begin] = arr[i].ves;
  105.        dlina += arr[i].ves;
  106.  
  107.     int old_color = color[arr[i].begin], new_color = color[arr[i].end];
  108.      for (int j = 0; j < n; j++)
  109.       if (color[j] == old_color)
  110.         color[j] = new_color;
  111.      }
  112.   }
  113. }
  114.  
  115. void Graph::get_result_kras()
  116. {
  117.   cout << dlina << endl;
  118.  
  119.   for (int i = 0; i < n; i++)
  120.    {
  121.      for(int j = 0; j < n; j++)
  122.       {
  123.         cout.width(3);
  124.         cout << path[i][j] << " ";
  125.       }
  126.       cout << endl;
  127.    }
  128. }
  129.  
  130. int main()
  131. {
  132.  int n, shet_reber, a, b, w;
  133.  cin >> n >> shet_reber;
  134.  
  135. rebro *arr = new rebro[shet_reber];
  136.  
  137. for (int i = 0; i < shet_reber; i++)
  138.   {
  139.     cin >> a >> b >> w;
  140.     a--;
  141.     b--;
  142.     arr[i].new_rebro(a, b, w);
  143.   }
  144.  
  145.  Graph object(arr, n, shet_reber);
  146.  object.sort_reber(0, shet_reber-1);
  147.  object.algorithm_kras();
  148.  object.get_result_kras();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement