Advertisement
Zhobra

Untitled

May 30th, 2022
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <windows.h>
  4. #include <list>
  5.  
  6. #define inf 100000
  7.  
  8. using namespace std;
  9.  
  10. struct Edges {
  11.     int a, b, w;
  12. };
  13.  
  14. struct Graph {
  15.     Edges* edge;
  16.     int* dist;
  17.     list<int>* way; //пути
  18.     int numVer; //число вершин
  19.     int index;
  20. };
  21.  
  22. void create(int m, Graph& G);
  23. void addEdge(int top1, int top2, int weight, Graph& G);
  24. void BellmanFord(int s, Graph& G);
  25. void Print(int s, Graph& G);
  26. void clear(Graph& G);
  27.  
  28.  
  29. int main() {
  30.     SetConsoleCP(1251);
  31.     SetConsoleOutputCP(1251);
  32.  
  33.     ifstream FileIn("in.txt", ios_base::in);
  34.     int read, ver, c = 0, weight, num;
  35.     FileIn >> c;
  36.     Graph G;
  37.     create(c, G);
  38.     do {
  39.         cout << "Введите номер вершины, расстояния от кроторой необходимо найти(0 - " << c - 1 << "): ";
  40.         cin >> num;
  41.     } while ((num < 0) || (num > (c - 1)));
  42.     c = 0;
  43.     while (!FileIn.eof()) {
  44.         if (c == 0) {
  45.             c = 1;
  46.             FileIn >> ver;
  47.         }
  48.         else if (FileIn.get() == '\n') {
  49.             c = 0;
  50.         }
  51.         else {
  52.             FileIn >> read;
  53.             FileIn >> weight;
  54.             addEdge(ver, read, weight, G);
  55.         }
  56.     }
  57.     BellmanFord(num, G);
  58.     Print(num, G);
  59.     clear(G);
  60.     system("Pause");
  61.     return 0;
  62. }
  63.  
  64.  
  65.  
  66. void create(int m, Graph& G) {
  67.     G.edge = new Edges[m * (m - 1)];
  68.     G.numVer = m;
  69.     G.way = new list<int>[m];
  70.     G.dist = new int[m];
  71.     G.index = 0;
  72. }
  73.  
  74. void addEdge(int a, int b, int weight, Graph& G) {
  75.     G.edge[G.index].a = a;
  76.     G.edge[G.index].b = b;
  77.     G.edge[G.index].w = weight;
  78.     G.index++;
  79. }
  80.  
  81. void BellmanFord(int s, Graph& G) {
  82.     int i, j;
  83.     for (i = 0; i < G.numVer; i++)
  84.         G.dist[i] = inf;
  85.     G.dist[s] = 0;
  86.     for (i = 0; i < G.numVer; ++i) {
  87.         for (j = 0; j < G.index; j++) {
  88.             if (G.dist[G.edge[j].a] + G.edge[j].w < G.dist[G.edge[j].b]) {
  89.                 G.dist[G.edge[j].b] = G.dist[G.edge[j].a] + G.edge[j].w;
  90.                 G.way[G.edge[j].b] = G.way[G.edge[j].a];
  91.                 G.way[G.edge[j].b].push_front(G.edge[j].a);
  92.                 G.way[G.edge[j].b].push_front(G.edge[j].b);
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. void Print(int s, Graph& G) {
  99.     for (int i = 0; i < G.numVer; i++) {
  100.         cout << s;
  101.         while (!G.way[i].empty()) {
  102.             if (G.way[i].back() == s);
  103.                 G.way[i].pop_back();
  104.             if (G.way[i].back() == i) {
  105.                 G.way[i].pop_back();
  106.                 if (G.way[i].empty())
  107.                     G.way[i].push_front(i);
  108.             }
  109.             cout << "->" << G.way[i].back();
  110.             G.way[i].pop_back();
  111.         }
  112.         cout << "\tРасстояние: " << G.dist[i] << endl;
  113.     }
  114. }
  115.  
  116. void clear(Graph& G) {
  117.     delete[] G.edge;
  118.     delete[] G.dist;
  119.     delete[] G.way;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement