zelleratumm

цйа

May 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     cout << "Enter vertex count: ";
  10.     int n;
  11.     cin >> n;
  12.  
  13.     const int INF = 1e9;
  14.  
  15.     //  ввод графа
  16.     cout << "Enter sprase matrix (n*n):\n";
  17.     int **spm = new int*[n];
  18.     for (int i = 0; i < n; ++i) {
  19.         spm[i] = new int[n];
  20.         for (int j = 0; j < n; ++j) {
  21.             cin >> spm[i][j];
  22.             if (spm[i][j] <= 0) {
  23.                 spm[i][j] = INF;
  24.             }
  25.             if (i == j) {
  26.                 spm[i][j] = 0;
  27.             }
  28.         }
  29.     }
  30.  
  31.     //  выбор алгоритма
  32.     int algo = -1;
  33.     while (algo != 0 && algo != 1) {
  34.         cout << "Choose algorithm.\n"
  35.                 "0 -> dijkstra\n"
  36.                 "1 -> floyd\n"
  37.                 "Your choice: ";
  38.         cin >> algo;
  39.     }
  40.  
  41.     if (algo == 0) {
  42.  
  43.         //  дейкстра
  44.  
  45.         cout << "Enter start vertex number: ";
  46.         int s;
  47.         cin >> s;
  48.  
  49.         int *path = new int[n], *visited = new int[n];
  50.         for (int i = 0; i < n; ++i) {
  51.             path[i] = INF;
  52.             visited[i] = false;
  53.         }
  54.  
  55.         path[s] = 0;
  56.         for (int i = 0; i < n; ++i) {
  57.             int cur = -1;
  58.             for (int j = 0; j < n; ++j) {
  59.                 if (!visited[j] && (cur == -1 || path[cur] > path[j])) {
  60.                     cur = j;
  61.                 }
  62.             }
  63.             if (path[cur] == INF) {
  64.                 break;
  65.             }
  66.             visited[cur] = true;
  67.             for (int i = 0; i < n; ++i) {
  68.                 if (spm[cur][i] != INF) {
  69.                     path[i] = min(path[i], path[cur] + spm[cur][i]);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         for (int i = 0; i < n; ++i) {
  75.             if (path[i] == INF) {
  76.                 cout << "INF ";
  77.             } else {
  78.                 cout << path[i] << " ";
  79.             }
  80.         }
  81.  
  82.     } else {
  83.  
  84.         //  флойд
  85.  
  86.         for (int i = 0; i < n; ++i) {
  87.             for (int j = 0; j < n; ++j) {
  88.                 for (int l = 0; l < n; ++l) {
  89.                     spm[i][j] = min(spm[i][j], spm[i][l] + spm[l][j]);
  90.                 }
  91.             }
  92.         }
  93.  
  94.         for (int i = 0; i < n; ++i) {
  95.             for (int j = 0; j < n; ++j) {
  96.                 if (spm[i][j] >= INF) {
  97.                     cout << "INF ";
  98.                 } else {
  99.                     cout << spm[i][j] << " ";
  100.                 }
  101.             }
  102.             cout << "\n";
  103.         }
  104.  
  105.     }
  106.  
  107.     cout << "\n";
  108.  
  109. }
Add Comment
Please, Sign In to add comment