Xom9ik

Alg_Lab_10 (lll semester)

Nov 28th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void Dijkstra(int* GR[], int st, int V)
  7. {
  8.     int *distance = new int[V];
  9.     int count, index, i, u, m = st + 1;
  10.     bool *visited = new bool[V];
  11.     for (i = 0; i<V; i++)
  12.     {
  13.         distance[i] = INT_MAX; visited[i] = false;
  14.     }
  15.     distance[st] = 0;
  16.     for (count = 0; count<V - 1; count++)
  17.     {
  18.         int min = INT_MAX;
  19.         for (i = 0; i<V; i++)
  20.             if (!visited[i] && distance[i] <= min)
  21.             {
  22.                 min = distance[i]; index = i;
  23.             }
  24.         u = index;
  25.         visited[u] = true;
  26.         for (i = 0; i<V; i++)
  27.             if (!visited[i] && GR[u][i] && distance[u] != INT_MAX &&
  28.                 distance[u] + GR[u][i]<distance[i])
  29.                 distance[i] = distance[u] + GR[u][i];
  30.     }
  31.     cout << "Стоимость пути из начальной вершины до остальных:\t\n";
  32.     for (i = 0; i<V; i++) if (distance[i] != INT_MAX)
  33.         cout << m << " > " << i + 1 << " = " << distance[i] << endl;
  34.     else cout << m << " > " << i + 1 << " = " << "маршрут недоступен" << endl;
  35. }
  36.  
  37. void Floyd(int* D[], int V)
  38. {
  39.     int k;
  40.     for (int i = 0; i<V; i++)
  41.         D[i][i] = 0;
  42.     for (int k = 0; k<V; k++)
  43.         for (int i = 0; i<V; i++)
  44.             for (int j = 0; j<V; j++)
  45.                 if (D[i][k] && D[k][j] && i != j)
  46.                     if (D[i][k] + D[k][j]<D[i][j] || D[i][j] == 0)
  47.                         D[i][j] = D[i][k] + D[k][j];
  48.  
  49.  
  50.     cout << setw(4);
  51.     for (int i = 0; i < V; i++)
  52.         cout << "|x" << i + 1;
  53.     cout << endl;
  54.     for (int i = 0; i < V; i++)
  55.     {
  56.         cout << "X" << i + 1 << '|';
  57.         for (int j = 0; j < V; j++)
  58.             cout << D[i][j] << "  ";
  59.         cout << endl;
  60.     }
  61. }
  62. int main()
  63. {
  64.     setlocale(LC_ALL, "Rus");
  65.     int V;
  66.     cout << "Введите размерность графа: ";
  67.     while (!(cin >> V) || (cin.peek() != '\n' || !(V >= 2)))
  68.     {
  69.         cin.clear();
  70.         while (cin.get() != '\n');
  71.         cout << "Введено недопустимое значение " << V << " Повторите попытку." << endl;
  72.     }
  73.     int **VES = new int*[V];
  74.     for (int i = 0; i < V; i++)
  75.         VES[i] = new int[V];
  76.     int VV = V*V;
  77.     int start = 0;;
  78.     cout << "Заполните матрицу весов графа " << endl;
  79.     cout << setw(4);
  80.     for (int i = 0; i < V; i++)
  81.         cout << "|x" << i + 1;
  82.     cout << endl;
  83.     for (int i = 0; i < V; i++)
  84.     {
  85.         cout << "X" << i + 1 << '|';
  86.         for (int j = 0; j < V; j++)
  87.             cin >> VES[i][j];
  88.     }
  89.     cout << "Начальная вершина >> ";
  90.     cin >> start;
  91.     cout << "Алгоритм Дейкстры" << endl;
  92.     Dijkstra(VES, start - 1, V);
  93.     cout << "Алгоритм Флойда" << endl;
  94.     Floyd(VES, V);
  95.     system("pause");
  96. }
Advertisement
Add Comment
Please, Sign In to add comment