vadimk772336

Untitled

Dec 19th, 2021
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 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;
  78.             EDG2.b = b;
  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.     for (int i = 0; i < n; ++i)
  113.     {
  114.         EDG = heap.top();
  115.  
  116.         heap.pop();
  117.  
  118.         cout << "Взял очередное мин ребро  " << EDG.a << " " <<  EDG.b << " " << EDG.cost << endl;
  119.         if (max < EDG.cost)
  120.         {
  121.             max = EDG.cost;
  122.             cout << "new max = " << max << endl;
  123.         }
  124.  
  125.         if (vertexes[EDG.a - 1].visited)
  126.         {
  127.             new_vertex = EDG.b - 1;
  128.             cout << EDG.a << " уже посещено поэтому новая вершина - " << EDG.b << endl;
  129.             vertexes[new_vertex].visited = true;
  130.         }
  131.         else
  132.         {
  133.             new_vertex = EDG.a - 1;
  134.             cout << EDG.b << " уже посещено поэтому новая вершина - " << EDG.a << endl;
  135.             vertexes[new_vertex].visited = true;
  136.         }
  137.        
  138.         cout << "new vert = " << new_vertex << endl;
  139.  
  140.         cout << " у new vert " << vertexes[new_vertex].count_edges << " рёбер, щас буду их добавлять " << endl;
  141.         for (int j = 0; j < vertexes[new_vertex].count_edges; ++j)
  142.         {
  143.  
  144.             EDG = vertexes[new_vertex].adj_list[j];
  145.             cout << "Добавляю ребро " << EDG.a << " " <<  EDG.b << " " << EDG.cost << " ... ";
  146.             if (not vertexes[EDG.idx].visited)
  147.             {
  148.                 //EDG.a = new_vertex;
  149.                 //EDG.b = EDG.idx;
  150.                 heap.push(EDG);
  151.                 cout << "ребро с концом в не посещенной вершиной - добавлено" << endl;
  152.                 showpq(heap);
  153.                
  154.             }
  155.             else
  156.                 cout << "ребро не подходит " << endl;
  157.         }
  158.         cout << " Закончил, на данный момент max = " << max << endl;
  159.     }
  160.    
  161.     cout << "max = " << max << endl;
  162.  
  163.  
  164.     return 0;
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment