zelleratumm

va32

May 31st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 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.     while (true) {
  32.  
  33.         //  выбор алгоритма
  34.         int algo = -1;
  35.         while (algo != 0 && algo != 1 && algo != 2 && algo != 3) {
  36.             cout << "Choose algorithm.\n"
  37.                     "0 -> dijkstra\n"
  38.                     "1 -> floyd\n"
  39.                     "2 -> dijkstra + limit\n"
  40.                     "3 -> exit\n"
  41.                     "Your choice: ";
  42.             cin >> algo;
  43.         }
  44.  
  45.         if (algo == 3) {
  46.             break;
  47.         }
  48.  
  49.         if (algo == 0 || algo == 2) {
  50.  
  51.             //  дейкстра
  52.  
  53.             int limit = -1;
  54.             if (algo == 2) {
  55.                 while (limit < 0) {
  56.                     cout << "Enter limit: ";
  57.                     cin >> limit;
  58.                 }
  59.             }
  60.  
  61.             cout << "Enter start vertex number: ";
  62.             int s;
  63.             cin >> s;
  64.  
  65.             int *path = new int[n], *visited = new int[n], *from = new int[n];
  66.             for (int i = 0; i < n; ++i) {
  67.                 path[i] = INF;
  68.                 visited[i] = false;
  69.                 from[i] = -1;
  70.             }
  71.  
  72.             path[s] = 0;
  73.             for (int i = 0; i < n; ++i) {
  74.                 int cur = -1;
  75.                 for (int j = 0; j < n; ++j) {
  76.                     if (!visited[j] && (cur == -1 || path[cur] > path[j])) {
  77.                         cur = j;
  78.                     }
  79.                 }
  80.                 if (path[cur] == INF) {
  81.                     break;
  82.                 }
  83.                 visited[cur] = true;
  84.                 for (int i = 0; i < n; ++i) {
  85.                     if (spm[cur][i] != INF && path[cur] + spm[cur][i] < path[i]) {
  86.                         from[i] = cur;
  87.                         path[i] = path[cur] + spm[cur][i];
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             if (algo == 2) {
  93.                 cout << "Path list lower than " << limit << ":\n";
  94.                 for (int i = 0; i < n; ++i) {
  95.                     if (s != i && path[i] < limit) {
  96.                         int tmp = i;
  97.                         while (tmp != s) {
  98.                             cout << tmp << " <- ";
  99.                             tmp = from[tmp];
  100.                         }
  101.                         cout << s << " = " << path[i] << "\n";
  102.                     }
  103.                 }
  104.             } else {
  105.                 for (int i = 0; i < n; ++i) {
  106.                     if (path[i] == INF) {
  107.                         cout << "INF ";
  108.                     } else {
  109.                         cout << path[i] << " ";
  110.                     }
  111.                 }
  112.             }
  113.  
  114.             delete[] path;
  115.             delete[] visited;
  116.             delete[] from;
  117.  
  118.         } else {
  119.  
  120.             //  флойд
  121.  
  122.             int **spm_tmp = new int*[n];
  123.             for (int i = 0; i < n; ++i) {
  124.                 spm_tmp[i] = new int[n];
  125.                 for (int j = 0; j < n; ++j) {
  126.                     spm_tmp[i][j] = spm[i][j];
  127.                 }
  128.             }
  129.  
  130.             for (int i = 0; i < n; ++i) {
  131.                 for (int j = 0; j < n; ++j) {
  132.                     for (int l = 0; l < n; ++l) {
  133.                         spm_tmp[i][j] = min(spm_tmp[i][j], spm_tmp[i][l] + spm_tmp[l][j]);
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             for (int i = 0; i < n; ++i) {
  139.                 for (int j = 0; j < n; ++j) {
  140.                     if (spm_tmp[i][j] >= INF) {
  141.                         cout << "INF ";
  142.                     } else {
  143.                         cout << spm_tmp[i][j] << " ";
  144.                     }
  145.                 }
  146.                 cout << "\n";
  147.             }
  148.  
  149.             for (int i = 0; i < n; ++i) {
  150.                 delete[] spm_tmp[i];
  151.             }
  152.             delete[] spm_tmp;
  153.  
  154.         }
  155.  
  156.         cout << "\n";
  157.  
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment