Advertisement
sacr1ficerq

A (virgin input)

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