Advertisement
den4ik2003

Untitled

Sep 27th, 2022
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5. using std::vector;
  6. using std::pair;
  7.  
  8. namespace constants {
  9. const long long kInf = 1e18;
  10. }
  11.  
  12. class Graph {
  13.  public:
  14.  
  15.   explicit Graph() = default;
  16.  
  17.   explicit Graph(int n, int m) : num_of_vertexes_(n), num_of_edges_(m), cheapest_vertex(0),
  18.                                  neighbours_(vector<vector<pair<int, long long>>>(n)) {
  19.     vector<long long> input_vertexes(n);
  20.     for (int i = 0; i < n; ++i) {
  21.       std::cin >> input_vertexes[i];
  22.       if (input_vertexes[i] < input_vertexes[cheapest_vertex]) {
  23.         cheapest_vertex = i;
  24.       }
  25.     }
  26.     for (int i = 0; i < n; ++i) {
  27.       if (i != cheapest_vertex) {
  28.         AddEdge(cheapest_vertex + 1, i + 1, input_vertexes[cheapest_vertex] + input_vertexes[i]);
  29.       }
  30.     }
  31.     for (int i = 0; i < m; ++i) {
  32.       int first, second;
  33.       long long weight;
  34.       std::cin >> first >> second >> weight;
  35.       AddEdge(first, second, weight);
  36.     }
  37.   }
  38.  
  39.   int GetCheapestVertex() {
  40.     return cheapest_vertex;
  41.   }
  42.  
  43.   void AddEdge(int first, int second, long long weight) {
  44.     neighbours_[first - 1].push_back({second - 1, weight});
  45.     neighbours_[second - 1].push_back({first - 1, weight});
  46.   }
  47.  
  48.   int GetNumOfVertexes() const {
  49.     return num_of_vertexes_;
  50.   }
  51.  
  52.   vector<vector<pair<int, long long>>> GetNeighbours() const {
  53.     return neighbours_;
  54.   }
  55.  
  56.  private:
  57.   int num_of_vertexes_;
  58.   int num_of_edges_;
  59.   int cheapest_vertex;
  60.   vector<vector<pair<int, long long>>> neighbours_;
  61. };
  62.  
  63. struct MSTBuilder {
  64.   virtual void GetMST(Graph&)  = 0;
  65. };
  66.  
  67. struct PrimoBuildMST : MSTBuilder {
  68.   long long result = 0;
  69.  
  70.   void GetMST(Graph& graph) override {  // часть этого кода позаимствована с e-maxx.ru, но там была неработающая версия + исрпавил ошибку
  71.     int n = graph.GetNumOfVertexes();
  72.     std::set<std::pair<long long, int>> s;
  73.     s.insert({0, graph.GetCheapestVertex()});
  74.     vector<vector<pair<int, long long>>> neighbours = graph.GetNeighbours();
  75.     vector<bool> used(n, false);
  76.     vector<long long> cheapest_edge(n, constants::kInf);
  77.     cheapest_edge[graph.GetCheapestVertex()] = 0;
  78.     for (int i = 0; i < n; ++i) {
  79.       int open_v = s.begin()->second;
  80.       used[open_v] = true;
  81.       result += s.begin()->first;
  82.       s.erase(s.begin());
  83.       for (auto neigh : neighbours[open_v]) {
  84.         int to = neigh.first;
  85.         long long edge = neigh.second;
  86.         if (edge < cheapest_edge[to] and !used[to]) {
  87.           s.erase({cheapest_edge[to], to});
  88.           cheapest_edge[to] = edge;
  89.           s.insert({edge, to});
  90.         }
  91.       }
  92.     }
  93.   }
  94. };
  95.  
  96. int main() {
  97.   int n;
  98.   int m;
  99.   std::cin >> n >> m;
  100.   Graph graph(n, m);
  101.   PrimoBuildMST primo;
  102.   primo.GetMST(graph);
  103.   std::cout << primo.result << "\n";
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement