Advertisement
myloyo

10. Веса IV c (Беллмана-Форда) (12 задание)

Dec 9th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. void Task10(string u, string v) {
  2.         if (!ExistVertex(u) || !ExistVertex(v)) {
  3.             cout << "Одна или обе вершины не существуют." << endl;
  4.             return;
  5.         }
  6.  
  7.         map<string, int> distances;
  8.         map<string, string> pred;
  9.         for (const auto& vertex : AdjList) {
  10.             distances[vertex.first] = INF;
  11.             pred[vertex.first] = "";
  12.         }
  13.         distances[u] = 0;
  14.  
  15.         // алгоритм Беллмана-Форда
  16.         int V = CountVertices();
  17.         for (int i = 0; i < V - 1; ++i) {
  18.             for (const auto& u : AdjList) {
  19.                 for (const auto& v : u.second) {
  20.                     string from = u.first;
  21.                     string to = v.first;
  22.                     int weight = v.second;
  23.  
  24.                     if (distances[from] < INF && distances[from] + weight < distances[to]) {
  25.                         distances[to] = distances[from] + weight;
  26.                         pred[to] = from;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.  
  32.         for (const auto& u : AdjList) {
  33.             for (const auto& v : u.second) {
  34.                 string from = u.first;
  35.                 string to = v.first;
  36.                 int weight = v.second;
  37.  
  38.                 if (distances[from] < INF && distances[from] + weight < distances[to]) {
  39.                     cout << "Граф содержит отрицательный цикл!" << endl;
  40.                     return;
  41.                 }
  42.             }
  43.         }
  44.  
  45.        
  46.         if (distances[v] == INF) {
  47.             cout << "Нет пути из " << u << " в " << v << "." << endl;
  48.             return;
  49.         }
  50.  
  51.         vector<string> path;
  52.         for (string at = v; at != ""; at = pred[at]) {
  53.             path.push_back(at);
  54.         }
  55.         reverse(path.begin(), path.end());
  56.  
  57.         cout << "Кратчайший путь из " << u << " в " << v << ": ";
  58.         for (size_t i = 0; i < path.size(); ++i) {
  59.             cout << path[i];
  60.             if (i < path.size() - 1) cout << " -> ";
  61.         }
  62.         cout << "\nДлина пути: " << distances[v] << "\n";
  63.     }
  64.  
  65.  
  66. //main.cpp
  67. #include <iostream>
  68. #include <string>
  69. #include <vector>
  70. #include <fstream>
  71. #include <vector>
  72. #include <list>
  73. #include <map>
  74. #include <locale>
  75. #include <windows.h>
  76. #include "Graph.h"
  77.  
  78. using namespace std;
  79.  
  80. int main() {
  81.     setlocale(LC_ALL, "Russian");
  82.     SetConsoleOutputCP(CP_UTF8);
  83.     map<string, Graph> graphs;
  84.  
  85.     string с = "prim3.txt";
  86.     Graph g;
  87.     g.loadFromFile(с);
  88.     graphs[с] = g;
  89.  
  90.     string u1 v;
  91.     cout << "введите вершины u1 и v:\n";
  92.     cin >> u1 >> v;
  93.  
  94.     cout << "Задание 10\n";
  95.     g.Task10(u1, v);
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement