zelleratumm

ц2й212

May 31st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 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 && algo != 2) {
  34.         cout << "Choose algorithm.\n"
  35.                 "0 -> dijkstra\n"
  36.                 "1 -> floyd\n"
  37.                 "2 -> dijkstra + limit\n"
  38.                 "Your choice: ";
  39.         cin >> algo;
  40.     }
  41.  
  42.     if (algo == 0 || algo == 2) {
  43.  
  44.         //  дейкстра
  45.  
  46.         int limit = -1;
  47.         if (algo == 2) {
  48.             while (limit < 0) {
  49.                 cout << "Enter limit: ";
  50.                 cin >> limit;
  51.             }
  52.         }
  53.  
  54.         cout << "Enter start vertex number: ";
  55.         int s;
  56.         cin >> s;
  57.  
  58.         int *path = new int[n], *visited = new int[n];
  59.         for (int i = 0; i < n; ++i) {
  60.             path[i] = INF;
  61.             visited[i] = false;
  62.         }
  63.  
  64.         path[s] = 0;
  65.         for (int i = 0; i < n; ++i) {
  66.             int cur = -1;
  67.             for (int j = 0; j < n; ++j) {
  68.                 if (!visited[j] && (cur == -1 || path[cur] > path[j])) {
  69.                     cur = j;
  70.                 }
  71.             }
  72.             if (path[cur] == INF) {
  73.                 break;
  74.             }
  75.             visited[cur] = true;
  76.             for (int i = 0; i < n; ++i) {
  77.                 if (spm[cur][i] != INF) {
  78.                     path[i] = min(path[i], path[cur] + spm[cur][i]);
  79.                 }
  80.             }
  81.         }
  82.  
  83.         if (algo == 2) {
  84.             cout << "Path list lower than " << limit << ":\n";
  85.             for (int i = 0; i < n; ++i) {
  86.                 if (s != i && path[i] < limit) {
  87.                     cout << s << " -> " << i << " = " << path[i] << "\n";
  88.                 }
  89.             }
  90.         } else {
  91.             for (int i = 0; i < n; ++i) {
  92.                 if (path[i] == INF) {
  93.                     cout << "INF ";
  94.                 } else {
  95.                     cout << path[i] << " ";
  96.                 }
  97.             }
  98.         }
  99.  
  100.     } else {
  101.  
  102.         //  флойд
  103.  
  104.         for (int i = 0; i < n; ++i) {
  105.             for (int j = 0; j < n; ++j) {
  106.                 for (int l = 0; l < n; ++l) {
  107.                     spm[i][j] = min(spm[i][j], spm[i][l] + spm[l][j]);
  108.                 }
  109.             }
  110.         }
  111.  
  112.         for (int i = 0; i < n; ++i) {
  113.             for (int j = 0; j < n; ++j) {
  114.                 if (spm[i][j] >= INF) {
  115.                     cout << "INF ";
  116.                 } else {
  117.                     cout << spm[i][j] << " ";
  118.                 }
  119.             }
  120.             cout << "\n";
  121.         }
  122.  
  123.     }
  124.  
  125.     cout << "\n";
  126.  
  127. }
Add Comment
Please, Sign In to add comment