Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <vector>
  4. #include <utility>
  5. #include <queue>
  6. #include <unordered_map>
  7.  
  8. struct IGraph {
  9.     virtual ~IGraph() {}
  10.  
  11.     // Добавление ребра от from к to.
  12.     virtual void AddEdge(int from, int to, int value) = 0;
  13.  
  14.     virtual int VerticesCount() const = 0;
  15.  
  16.     virtual void GetNextVertices(int vertex, std::vector<std::pair<int, int>> &vertices) const = 0;
  17.     virtual void GetPrevVertices(int vertex, std::vector<std::pair<int, int>> &vertices) const = 0;
  18. };
  19.  
  20. struct ListGraph : public IGraph {
  21.     explicit ListGraph(int count);
  22.     ListGraph(const IGraph& graph);
  23.     virtual ~ListGraph() = default;
  24.  
  25.     // Добавление ребра от from к to.
  26.     virtual void AddEdge(int from, int to, int value);
  27.  
  28.     virtual int VerticesCount() const;
  29.     virtual void GetNextVertices(int vertex, std::vector<std::pair<int, int>>& vertices) const;
  30.     virtual void GetPrevVertices(int vertex, std::vector<std::pair<int, int>>& vertices) const;
  31.  
  32. private:
  33.     std::vector<std::vector<std::pair<int, int>>> Matrix;
  34. };
  35.  
  36. ListGraph::ListGraph(int count) {
  37.     Matrix.resize(count);
  38. };
  39.  
  40. ListGraph::ListGraph(const IGraph& graph) {
  41.     Matrix.resize(graph.VerticesCount());
  42.     for (int i = 0; i < Matrix.size(); i++) {
  43.         graph.GetNextVertices(i, Matrix[i]);
  44.     }
  45. };
  46.  
  47. void ListGraph::AddEdge(int from, int to, int value) {
  48.     assert(from >= 0 && from < Matrix.size());
  49.     assert(to >= 0 && to < Matrix.size());
  50.     Matrix[from].push_back({ to, value });
  51. };
  52.  
  53. int ListGraph::VerticesCount() const {
  54.     return Matrix.size();
  55. };
  56.  
  57. void ListGraph::GetNextVertices(int vertex, std::vector<std::pair<int, int>>& vertices) const {
  58.     assert(vertex >= 0 && vertex < Matrix.size());
  59.     vertices = Matrix[vertex];
  60. };
  61.  
  62. void ListGraph::GetPrevVertices(int vertex, std::vector<std::pair<int, int>>& vertices) const {
  63.     assert(vertex >= 0 && vertex < Matrix.size());
  64.     vertices.clear();
  65.     for (int i = 0; i < Matrix.size(); i++) {
  66.         for (int j = 0; j < Matrix[i].size(); j++) {
  67.             if (Matrix[i][j].first == vertex) {
  68.                 vertices.push_back({ i, Matrix[i][j].second });
  69.                 break;
  70.             }
  71.         }
  72.     }
  73. };
  74.  
  75. bool compare(const std::pair<int, int> &a, const std::pair<int, int> &b) {
  76.     return a.second < b.second;
  77. }
  78.  
  79. int min_tree(IGraph &input) {
  80.     std::vector<int> weight = std::vector<int>(input.VerticesCount(), INT_MAX);
  81.     int ans = 0;
  82.     weight[0] = 0;
  83.     std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(&compare)> list(&compare);
  84.     list.push({ 0, 0 });
  85.     while (!list.empty()) {
  86.         std::pair<int, int> v = list.top();
  87.         list.pop();
  88.         std::vector<std::pair<int, int>> transfer;
  89.         input.GetNextVertices(v.first, transfer);
  90.         for (std::pair<int, int> u : transfer){
  91.             if (weight[u.first] != INT_MAX)
  92.                 ans -= weight[u.first];
  93.             if (weight[u.first] > u.second) {
  94.                 int w = u.second;
  95.                 weight[u.first] = w;
  96.                 list.push({ u.first, w });
  97.             }
  98.             ans += weight[u.first];
  99.         }
  100.     }
  101.     return ans;
  102. }
  103.  
  104. int main() {
  105.     int n;
  106.     std::cin >> n;
  107.     ListGraph input(n);
  108.     std::cin >> n;
  109.     int from, to, value;
  110.     for (int i = 0; i < n; i++) {
  111.         std::cin >> from >> to >> value;
  112.         from -= 1;
  113.         to -= 1;
  114.         input.AddEdge(from, to, value);
  115.         input.AddEdge(to, from, value);
  116.     }
  117.  
  118.     std::cout << min_tree(input);
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement