Advertisement
sacr1ficerq

A

Apr 22nd, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Vertex;
  8.  
  9. struct Edge{
  10.     Vertex* neighbour;
  11.     int weight;
  12. };
  13.  
  14. static const size_t INF = 1 << 30;
  15.  
  16.  
  17. struct Vertex{
  18.     bool visited = false;
  19.     size_t distance = INF;
  20.  
  21.     void add_edge(Vertex* neighbour, int weight){
  22.         out_edges_.push_back({neighbour, weight});
  23.     }
  24.  
  25.     vector<Edge> get_neighbours(){
  26.         return out_edges_;
  27.     };
  28.  
  29.  
  30. private:
  31.     vector<Edge> out_edges_;
  32. };
  33.  
  34. class WeightedGraph{
  35. public:
  36.     explicit WeightedGraph(int v) : graph_(v+1), v_amount_(v){}
  37.  
  38.     void add_edge(int a, int b, int weight){
  39.         graph_[a+1].add_edge(&graph_[b+1], weight);
  40.     }
  41.  
  42.  
  43.     size_t get_path_length(int start, int finish){
  44.         //TODO dijkstra algorithm
  45.         // 1) get minimum distance vertex (linear complexity) done
  46.         // 2) relaxation (done)
  47.         graph_[start].distance = 0;
  48.         graph_[start].visited = true;
  49.         relaxation(start);
  50.  
  51.         for (int step = 0; step < v_amount_-1; ++step){
  52.             auto mn = find_minimum_distance_vertex();
  53.             relaxation(mn);
  54.         }
  55.  
  56.         auto result = graph_[finish].distance;
  57.         return result;
  58.     }
  59.  
  60. private:
  61.     int find_minimum_distance_vertex(){
  62.         size_t mn = INF;
  63.         int result = -1;
  64.         for (int i = 1; i < v_amount_+1; ++i){
  65.             if (!graph_[i].visited && graph_[i].distance < mn){
  66.                 mn = graph_[i].distance;
  67.                 result = i;
  68.             }
  69.         }
  70.         return result;
  71.     }
  72.  
  73.     void relaxation(int v_ind){
  74.         auto v = &graph_[v_ind];
  75.         v->visited = true;
  76.         for (auto [neighbour, weight] : v->get_neighbours()){
  77.             if (neighbour->distance > v->distance + weight){
  78.                 neighbour->distance = v->distance + weight;
  79.             }
  80.         }
  81.     }
  82.  
  83.     vector<Vertex> graph_;
  84.     int v_amount_;
  85. };
  86.  
  87.  
  88. int main(){
  89.     int v_amount, start, finish;
  90.     scanf("%d%d%d",&v_amount, &start, &finish);
  91.  
  92.     WeightedGraph graph(v_amount);
  93.  
  94.     for (int v = 0; v < v_amount; ++v){
  95.         for (int neighbour = 0; neighbour < v_amount; ++neighbour){
  96.             int weight = 0;
  97.             scanf("%d", &weight);
  98.  
  99.             if (neighbour == v){
  100.                 continue;
  101.             }
  102.             if (weight != -1){
  103.                 graph.add_edge(v, neighbour, weight);
  104.             }
  105.         }
  106.     }
  107.  
  108. //    WeightedGraph graph(6);
  109. //    int start = 1;
  110. //    int finish = 6  ;
  111. //
  112. //    for (int i = 0; i < 7; ++i){
  113. //        int a, b, weight;
  114. //        cin >> a >> b >> weight;
  115. //        graph.add_edge(a-1, b-1, weight);
  116. //    }
  117.  
  118.  
  119.     size_t path_length = graph.get_path_length(start, finish);
  120.  
  121.     if (path_length == INF){
  122.         cout << -1;
  123.         return 0;
  124.     }
  125.     cout << path_length;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement