vadimk772336

работает, принты

Dec 19th, 2021
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. struct edge
  6. {
  7.     int idx;
  8.     int a;
  9.     int b;
  10.     int cost;
  11. };
  12.  
  13. struct vertex
  14. {
  15.     bool visited = false;
  16.     std::vector<struct edge> adj_list = {};
  17.     int count_edges = 0;
  18. };
  19.  
  20. void addEdge(int i, int j, int cost, vector<vertex>& vertexes)
  21. {
  22.     edge buff;
  23.  
  24.     buff.idx = j;
  25.     buff.cost = cost;
  26.     buff.a = i;
  27.     buff.b = j;
  28.  
  29.     vertexes[i].adj_list.push_back(buff);
  30.  
  31.     buff.idx = i;
  32.     vertexes[j].adj_list.push_back(buff);
  33.  
  34.     vertexes[i].count_edges++;
  35.     vertexes[j].count_edges++;
  36. }
  37.  
  38.  
  39. struct Compare
  40. {
  41.     bool operator()(edge& o1, edge& o2) const
  42.     {
  43.         return o1.cost > o2.cost;
  44.     }
  45. };
  46.  
  47. void showpq(priority_queue<struct edge, vector<struct edge>, Compare> gq)
  48. {
  49.     priority_queue<struct edge, vector<struct edge>, Compare> g = gq;
  50.     while (!g.empty())
  51.     {
  52.         cout << '\t' << g.top().cost;
  53.         g.pop();
  54.     }
  55.     cout << '\n';
  56. }
  57.  
  58. int main()
  59. {
  60.     priority_queue<struct edge, vector<struct edge>, Compare> heap;
  61.  
  62.     int n, m;
  63.     cin >> n >> m;
  64.  
  65.     int a, b, c;
  66.     vector<struct vertex> vertexes(n);
  67.     struct edge EDG2;
  68.  
  69.     for (int i = 0; i < m; ++i)
  70.     {
  71.         cin >> a >> b >> c;
  72.  
  73.         addEdge(a - 1, b - 1, c, vertexes);
  74.  
  75.         if (a == 1 | b == 1)
  76.         {
  77.             EDG2.a = a - 1;
  78.             EDG2.b = b - 1;
  79.             EDG2.cost = c;
  80.             heap.push(EDG2);
  81.         }
  82.     }
  83.  
  84.     showpq(heap);
  85.  
  86.  
  87.     cout << "\n print:" << endl;
  88.     for (int i = 0; i < n; i++)
  89.     {
  90.         int size = vertexes[i].adj_list.size();
  91.  
  92.         cout << "idx =: ";
  93.         for (int j = 0; j < size; ++j)
  94.         {
  95.             cout << vertexes[i].adj_list[j].a << " " << vertexes[i].adj_list[j].b << " ;";
  96.         }
  97.  
  98.  
  99.         cout << ";   edge_value =: ";
  100.         for (int j = 0; j < size; ++j)
  101.         {
  102.             cout << vertexes[i].adj_list[j].cost << " ";
  103.         }
  104.         cout << endl;
  105.     }
  106.  
  107.     vertexes[0].visited = true;
  108.     struct edge EDG;
  109.     int new_vertex;
  110.     int max = 0;
  111.  
  112.  
  113.     for (int i = 0; i < n - 1; ++i)
  114.     {
  115.  
  116.         EDG = heap.top();
  117.  
  118.         heap.pop();
  119.         cout << "Взял очередное мин ребро  " << EDG.a + 1 << " " << EDG.b + 1 << " " << EDG.cost
  120.              << endl;
  121.         if (max < EDG.cost)
  122.         {
  123.             max = EDG.cost;
  124.             cout << "new max = " << max << endl;
  125.         }
  126.  
  127.  
  128.         if (vertexes[EDG.a].visited)
  129.         {
  130.             new_vertex = EDG.b;
  131.             cout << EDG.a + 1 << " уже посещено поэтому новая вершина - " << EDG.b + 1 << endl;
  132.             vertexes[new_vertex].visited = true;
  133.         }
  134.         else
  135.         {
  136.             new_vertex = EDG.a;
  137.             cout << EDG.b + 1 << " уже посещено поэтому новая вершина - " << EDG.a + 1 << endl;
  138.             vertexes[new_vertex].visited = true;
  139.         }
  140.  
  141.         cout << "new vert = " << new_vertex + 1 << endl;
  142.  
  143.         cout << " у new vert " << vertexes[new_vertex].count_edges
  144.              << " рёбер, щас буду их добавлять " << endl;
  145.         for (int j = 0; j < vertexes[new_vertex].count_edges; ++j)
  146.         {
  147.  
  148.             EDG = vertexes[new_vertex].adj_list[j];
  149.             cout << "Добавляю ребро " << EDG.a + 1 << " " << EDG.b + 1 << " " << EDG.cost
  150.                  << " ... ";
  151.             if (not vertexes[EDG.idx].visited)
  152.             {
  153.                 // EDG.a = new_vertex;
  154.                 // EDG.b = EDG.idx;
  155.                 heap.push(EDG);
  156.                 cout << "ребро с концом в не посещенной вершиной - добавлено" << endl;
  157.                 showpq(heap);
  158.             }
  159.             else
  160.                 cout << "ребро не подходит " << endl;
  161.         }
  162.         cout << " Закончил, на данный момент max = " << max << endl;
  163.     }
  164.  
  165.     cout << "max = " << max << endl;
  166.  
  167.  
  168.     return 0;
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment