vadimk772336

принята

Dec 19th, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. struct edge
  5. {
  6.     int a;
  7.     int b;
  8.     int c;
  9. };
  10.  
  11. struct adjacent_vertex
  12. {
  13.     int c;
  14.     int b;
  15. };
  16.  
  17. struct vertex
  18. {
  19.     bool visited = false;
  20.     std::vector<struct adjacent_vertex> adj_list;
  21.     int count_edges = 0;
  22. };
  23.  
  24. void addEdge(int i, int j, int c, std::vector<vertex>& vertexes)
  25. {
  26.     adjacent_vertex buff;
  27.  
  28.     buff.b = j;
  29.     buff.c = c;
  30.  
  31.     vertexes[i].adj_list.push_back(buff);
  32.  
  33.     buff.b = i;
  34.     vertexes[j].adj_list.push_back(buff);
  35.  
  36.     vertexes[i].count_edges++;
  37.     vertexes[j].count_edges++;
  38. }
  39.  
  40.  
  41. struct Compare
  42. {
  43.     bool operator()(edge& o1, edge& o2) const
  44.     {
  45.         return o1.c > o2.c;
  46.     }
  47. };
  48.  
  49. int main()
  50. {
  51.     std::priority_queue<struct edge, std::vector<struct edge>, Compare> heap;
  52.  
  53.     int n, m;
  54.     std::cin >> n >> m;
  55.  
  56.     int a, b, c;
  57.     std::vector<struct vertex> vertexes(n);
  58.     struct edge EDG2;
  59.  
  60.     for (int i = 0; i < m; ++i)
  61.     {
  62.         std::cin >> a >> b >> c;
  63.  
  64.         addEdge(a - 1, b - 1, c, vertexes);
  65.  
  66.         if (a == 1 | b == 1)
  67.         {
  68.             EDG2.a = a - 1;
  69.             EDG2.b = b - 1;
  70.             EDG2.c = c;
  71.             heap.push(EDG2);
  72.         }
  73.     }
  74.  
  75.     vertexes[0].visited = true;
  76.     struct edge EDG;
  77.     int new_vertex;
  78.     int max = 0;
  79.  
  80.     for (int i = 0; i < n - 1; ++i)
  81.     {
  82.  
  83.         EDG = heap.top();
  84.  
  85.         while (vertexes[EDG.a].visited & vertexes[EDG.b].visited)
  86.         {
  87.             heap.pop();
  88.             EDG = heap.top();
  89.         }
  90.  
  91.         heap.pop();
  92.  
  93.         if (max < EDG.c)
  94.             max = EDG.c;
  95.  
  96.         if (vertexes[EDG.a].visited)
  97.         {
  98.             new_vertex = EDG.b;
  99.             vertexes[new_vertex].visited = true;
  100.         }
  101.         else
  102.         {
  103.             new_vertex = EDG.a;
  104.             vertexes[new_vertex].visited = true;
  105.         }
  106.  
  107.  
  108.         for (int j = 0; j < vertexes[new_vertex].count_edges; ++j)
  109.         {
  110.             EDG.a = new_vertex;
  111.             EDG.b = vertexes[new_vertex].adj_list[j].b;
  112.             EDG.c = vertexes[new_vertex].adj_list[j].c;
  113.  
  114.             if (not vertexes[EDG.b].visited)
  115.                 heap.push(EDG);
  116.         }
  117.     }
  118.  
  119.     std::cout << max;
  120.  
  121.     return 0;
  122. }
  123.  
Add Comment
Please, Sign In to add comment