Advertisement
myloyo

9. Веса IV b (Флойда-Уоршелла) (17 задание)

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