zelleratumm

wevfq

May 31st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 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], *from = new int[n];
  59.         for (int i = 0; i < n; ++i) {
  60.             path[i] = INF;
  61.             visited[i] = false;
  62.             from[i] = -1;
  63.         }
  64.  
  65.         path[s] = 0;
  66.         for (int i = 0; i < n; ++i) {
  67.             int cur = -1;
  68.             for (int j = 0; j < n; ++j) {
  69.                 if (!visited[j] && (cur == -1 || path[cur] > path[j])) {
  70.                     cur = j;
  71.                 }
  72.             }
  73.             if (path[cur] == INF) {
  74.                 break;
  75.             }
  76.             visited[cur] = true;
  77.             for (int i = 0; i < n; ++i) {
  78.                 if (spm[cur][i] != INF && path[cur] + spm[cur][i] < path[i]) {
  79.                     from[i] = cur;
  80.                     path[i] = path[cur] + spm[cur][i];
  81.                 }
  82.             }
  83.         }
  84.  
  85.         if (algo == 2) {
  86.             cout << "Path list lower than " << limit << ":\n";
  87.             for (int i = 0; i < n; ++i) {
  88.                 if (s != i && path[i] < limit) {
  89.                     int tmp = i;
  90.                     while (tmp != s) {
  91.                         cout << tmp << " <- ";
  92.                         tmp = from[tmp];
  93.                     }
  94.                     cout << s << " = " << path[i] << "\n";
  95.                 }
  96.             }
  97.         } else {
  98.             for (int i = 0; i < n; ++i) {
  99.                 if (path[i] == INF) {
  100.                     cout << "INF ";
  101.                 } else {
  102.                     cout << path[i] << " ";
  103.                 }
  104.             }
  105.         }
  106.  
  107.     } else {
  108.  
  109.         //  флойд
  110.  
  111.         for (int i = 0; i < n; ++i) {
  112.             for (int j = 0; j < n; ++j) {
  113.                 for (int l = 0; l < n; ++l) {
  114.                     spm[i][j] = min(spm[i][j], spm[i][l] + spm[l][j]);
  115.                 }
  116.             }
  117.         }
  118.  
  119.         for (int i = 0; i < n; ++i) {
  120.             for (int j = 0; j < n; ++j) {
  121.                 if (spm[i][j] >= INF) {
  122.                     cout << "INF ";
  123.                 } else {
  124.                     cout << spm[i][j] << " ";
  125.                 }
  126.             }
  127.             cout << "\n";
  128.         }
  129.  
  130.     }
  131.  
  132.     cout << "\n";
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment