Advertisement
myloyo

8. Веса IV а (Дейкстра) (15 задание)

Dec 9th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. void Dijkstra(string start, map<string, int>& distances, map<string, string>& previous) {
  2.         for (const auto& vertex : this->AdjList) {
  3.             distances[vertex.first] = INT_MAX;
  4.         }
  5.         distances[start] = 0;
  6.  
  7.         priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> pq;
  8.         pq.push({ 0, start });
  9.  
  10.         while (!pq.empty()) {
  11.             string current = pq.top().second;
  12.             int dist = pq.top().first;
  13.             pq.pop();
  14.  
  15.             if (dist > distances[current]) continue;
  16.  
  17.             // Рассматриваем всех соседей текущей вершины
  18.             for (const auto& neighbor : this->AdjList[current]) {
  19.                 string next = neighbor.first;
  20.                 int weight = neighbor.second;
  21.                 int newDist = dist + weight;
  22.  
  23.                 if (newDist < distances[next]) {
  24.                     distances[next] = newDist;
  25.                     previous[next] = current;
  26.                     pq.push({ newDist, next });
  27.                 }
  28.             }
  29.         }
  30.     }
  31.  
  32.     void reconstructPath(map<string, string>& previous, string start, string end, vector<string>& path) {
  33.         for (string i = end; i != start; i = previous[i]) {
  34.             path.push_back(i);
  35.         }
  36.         path.push_back(start);
  37.         reverse(path.begin(), path.end());
  38.     }
  39.  
  40.     void Task8(string u1, string u2, string v) {
  41.         map<string, int> distU1, distU2;
  42.         map<string, string> prevU1, prevU2;
  43.  
  44.         Dijkstra(u1, distU1, prevU1);
  45.         Dijkstra(u2, distU2, prevU2);
  46.  
  47.         if (distU1[v] == INT_MAX || distU2[v] == INT_MAX) {
  48.             cout << "Пути из одной или обеих вершин до " << v << " не существуют." << endl;
  49.             return;
  50.         }
  51.  
  52.         vector<string> pathU1, pathU2;
  53.         reconstructPath(prevU1, u1, v, pathU1);
  54.         reconstructPath(prevU2, u2, v, pathU2);
  55.  
  56.         cout << "Кратчайший путь от " << u1 << " до " << v << ": ";
  57.         for (const auto& node : pathU1) {
  58.             cout << node << " ";
  59.         }
  60.         cout << "\nДлина пути: " << distU1[v] << endl;
  61.  
  62.         cout << "Кратчайший путь от " << u2 << " до " << v << ": ";
  63.         for (const auto& node : pathU2) {
  64.             cout << node << " ";
  65.         }
  66.         cout << "\nДлина пути: " << distU2[v] << endl;
  67.  
  68.         cout << "\n";
  69.     }
  70.  
  71. #include <iostream>
  72. #include <string>
  73. #include <vector>
  74. #include <fstream>
  75. #include <vector>
  76. #include <list>
  77. #include <map>
  78. #include <locale>
  79. #include <windows.h>
  80. #include "Graph.h"
  81.  
  82. using namespace std;
  83.  
  84. int main() {
  85.     setlocale(LC_ALL, "Russian");
  86.     SetConsoleOutputCP(CP_UTF8);
  87.     map<string, Graph> graphs;
  88.  
  89.     string с = "prim3.txt";
  90.     Graph g;
  91.     g.loadFromFile(с);
  92.     graphs[с] = g;
  93.  
  94.     string u1, u2, v;
  95.     cout << "введите вершины u1 u2 и v:\n";
  96.     cin >> u1 >> u2 >> v;
  97.    
  98.     cout << "Задание 8\n";
  99.     g.Task8(u1, u2, v);
  100.     cout << "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement