Advertisement
igoryanchik

Dijkstra's

Nov 20th, 2024 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <chrono>
  8.  
  9.  
  10. using namespace std;
  11. using vvi = vector<vector<int>>;
  12.  
  13. const int INF = 1e9;
  14. vector<int> dijkstra_with_vector(const vvi& graph, int start)
  15. {
  16.     int n = graph.size();
  17.     vector<int> dist(n, INF);
  18.     vector<bool> visited(n, 0);
  19.     vector<int> q = { start };
  20.  
  21.     dist[start] = 0;
  22.  
  23.     for (int i = 0; i < n - 1; ++i)
  24.     {
  25.         int u = -1;
  26.         for (int j = 0; j < n; ++j)
  27.             if (!visited[j] && (u == -1 || dist[j] < dist[u]))
  28.                 u = j;
  29.  
  30.         visited[u] = 1;
  31.  
  32.         for (int j = 0; j < n; ++j)
  33.             if (graph[u][j] != 0 && !visited[j] && dist[u] != INF)
  34.                 if (dist[u] + graph[u][j] < dist[j])
  35.                     dist[j] = dist[u] + graph[u][j];
  36.     }
  37.  
  38.  
  39.  
  40.     return dist;
  41. }
  42.  
  43. vector<int> dijkstra_with_queue(const vvi& graph, int start)
  44. {
  45.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  46.     vector<int> dist(graph.size(), INF);
  47.     vector<bool> visited(graph.size(), 0);
  48.  
  49.     q.push({ 0, start });
  50.     dist[start] = 0;
  51.  
  52.  
  53.     while (!q.empty())
  54.     {
  55.         int u = q.top().second;
  56.         int dist_u = q.top().first;
  57.         q.pop();
  58.  
  59.         if (dist_u > dist[u]) continue;
  60.  
  61.         for (int i = 0; i < graph[u].size(); ++i)
  62.         {
  63.             if (graph[u][i] != 0 &&  dist[i] > dist_u + graph[u][i])
  64.             {
  65.                 dist[i] = dist_u + graph[u][i];
  66.                 q.push({ dist[i], i });
  67.             }
  68.         }
  69.     }
  70.     return dist;
  71. }
  72.  
  73.  
  74. vector<vector<int>> get_data(int n)
  75. {
  76.     vector<vector<int>> graph(n, vector<int>(n, 0));
  77.     string line;
  78.  
  79.     for (int i = 0; i < n; ++i)
  80.     {
  81.         getline(cin, line);
  82.         istringstream ss(line);
  83.         string edge;
  84.  
  85.         while (ss >> edge)
  86.         {
  87.             size_t comma_pos = edge.find(',');
  88.             int neighbor = stoi(edge.substr(0, comma_pos)) - 1;
  89.             int weight = stoi(edge.substr(comma_pos + 1));
  90.             graph[i][neighbor] = weight;
  91.         }
  92.     }
  93.  
  94.     return graph;
  95. }
  96.  
  97. vvi get_data_from_file(const string& filename, int n)
  98. {
  99.     ifstream infile(filename);
  100.     vector<vector<int>> graph(n, vector<int>(n, 0));
  101.  
  102.     string line;
  103.     for (int i = 0; i < n; ++i)
  104.     {
  105.         if (!getline(infile, line)) {
  106.             cerr << "Error: Not enough lines in the file." << endl;
  107.             break;
  108.         }
  109.  
  110.         istringstream ss(line);
  111.         string edge;
  112.  
  113.         while (ss >> edge)
  114.         {
  115.             size_t comma_pos = edge.find(',');
  116.             int neighbor = stoi(edge.substr(0, comma_pos)) - 1;
  117.             int weight = stoi(edge.substr(comma_pos + 1));
  118.             graph[i][neighbor] = weight;
  119.         }
  120.     }
  121.  
  122.     infile.close();
  123.     return graph;
  124. }
  125.  
  126. int main()
  127. {
  128.     setlocale(LC_ALL, "ru");
  129.  
  130.     int n;
  131.     cin >> n;
  132.     int s, k;
  133.     cin >> s >> k;
  134.     s--; k--;
  135.  
  136.  
  137.     string fname = "graph.txt";
  138.     vvi graph = get_data_from_file(fname, n);
  139.  
  140.  
  141.     auto start = chrono::high_resolution_clock::now();
  142.     vector<int> dist = dijkstra_with_vector(graph, s);
  143.     auto end = chrono::high_resolution_clock::now();
  144.     chrono::duration<double>  rez = end - start;
  145.     cout << "Время работы обычного алгоритма Дейкстры: " << rez << "\nОтвет: " << dist[k] << '\n';
  146.  
  147.     start = chrono::high_resolution_clock::now();
  148.     dist = dijkstra_with_queue(graph, s);
  149.     end = chrono::high_resolution_clock::now();
  150.     rez = end - start;
  151.     cout << "Время алгоритма Дейкстры с использованием очереди с приоритетом: " << rez << "\nОтвет: " << dist[k] << '\n';
  152.  
  153.     return 0;
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement