Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <locale.h>
  5.  
  6. #define INF 100000
  7. #define SIZE 15
  8. using namespace std;
  9.  
  10. int main() {
  11.     setlocale(LC_ALL, "rus");
  12.     int d[SIZE];
  13.     int v[SIZE];
  14.     int G[SIZE][SIZE] = {
  15.         {0, 11, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0},
  16.         {0, 0, 0, 0, 0, 3, 7, 0, 0, 0, 0, 0, 0, 0, 0},
  17.         {0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  18.         {0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0},
  19.         {0, 0, 13, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0},
  20.         {0, 0, 7, 0, 5, 0, 0, 0, 3, 7, 0, 0, 0, 0, 0},
  21.         {0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  22.         {10, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  23.         {0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0},
  24.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0},
  25.         {0, 0, 0, 0, 0, 0, 15, 3, 0, 0, 0, 0, 0, 0, 0},
  26.         {0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0},
  27.         {0, 0, 0, 0, 13, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0},
  28.         {0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 3, 3, 0, 3},
  29.         {0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0}
  30.     };
  31.  
  32.     cout << "Матрица смежности:" << endl << endl;
  33.     for (int i = -1; i < SIZE; i++) {
  34.         for (int j = -1; j < SIZE; j++) {
  35.             if (i == -1 && j == -1) cout << setw(4) << "";
  36.             else if (i == -1)
  37.                 if (j < 10) cout << "___" << j; else cout << "__" << j;
  38.             else if (j == -1) cout << setw(3) << i << "|";
  39.             else cout << setw(4) << G[i][j];
  40.         }
  41.         cout << endl;
  42.     }
  43.  
  44.     int start;
  45. loop:
  46.     cout << endl << "Введите номер вершины от 0 до " << SIZE - 1 << ", из которой хотите найти кратчайший путь: ";
  47.     cin >> start;
  48.     if (start < 0 || start > SIZE - 1)
  49.     {
  50.         cout << endl << "Выбранная вершина не существует!" << endl;
  51.         goto loop;
  52.     }
  53.     cout << endl;
  54.     for (int i = 0; i < SIZE; i++)
  55.     {
  56.         d[i] = INF;
  57.         v[i] = -1;
  58.     }
  59.  
  60.     d[start] = 0;
  61.     for (int k = 0; k < SIZE - 1; k++)
  62.         for (int i = 0; i < SIZE; i++)
  63.             for (int j = 0; j < SIZE; j++)
  64.                 if (G[i][j] != 0 && (d[i] + G[i][j]) < d[j])
  65.                 {
  66.                     d[j] = d[i] + G[i][j];
  67.                     v[j] = i;
  68.                 }
  69.  
  70.     cout << setw(19) << "Вершина:";
  71.     for (int i = 0; i < SIZE; i++)
  72.         cout << setw(4) << i;
  73.     cout << endl << setw(19) << "Предыдущая вершина:";
  74.     for (int i = 0; i < SIZE; i++)
  75.         cout << setw(4) << v[i];
  76.     cout << endl << endl;
  77.  
  78.     for (int i = 0; i < SIZE; i++)
  79.         if (i == start) cout << "Вершина " << start << " - начальная." << endl;
  80.         else if (d[i] == INF) cout << "Пути из вершины " << start << " в вершину " << i << " не существует." << endl;
  81.         else cout << "Кратчайшее расстояние из вершины " << start << " в вершину " << i << " равно " << d[i] << ", предпоследняя вершина пути: " << v[i] << endl;
  82.     cout << endl;
  83.  
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement