spacerose

лаба5 аод черновик (работает)

Nov 13th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #define inf 100000
  3. using namespace std;
  4.  
  5. struct Edges {
  6.     int u, v, w;
  7. };
  8.  
  9. const int Vmax = 1000;
  10. const int Emax = Vmax * (Vmax - 1) / 2;
  11. int i, j, n, e, start;
  12. Edges edge[Emax];
  13. int d[Vmax];
  14.  
  15. void bellman_ford(int n, int s)
  16. {
  17.     int i, j;
  18.  
  19.     for (i = 0; i < n; i++) d[i] = inf;
  20.     d[s] = 0;
  21.  
  22.     for (i = 0; i < n - 1; i++)
  23.        for (j = 0; j < e; j++)
  24.        
  25.             if ((d[edge[j].v] + edge[j].w < d[edge[j].u]) && (d[edge[j].v] + edge[j].w >=0))
  26.                 d[edge[j].u] = d[edge[j].v] + edge[j].w;
  27.  
  28.    for (i = 0; i < n; i++)
  29.     //if (d[i] == inf)
  30.      //   cout << start << " и " << i + 1 << " не соединены между собой" << "\n"; else
  31.         cout << endl << start << "->" << i + 1 << "=" << d[i]<<"\n";
  32. }
  33. //главная функция
  34. int main()
  35. {
  36.     setlocale(LC_ALL, "");
  37.     int w;
  38.  
  39.     cout << "Количество вершин: ";
  40.     cin >> n;
  41.     e = 0;
  42.     for (i = 0; i < n; i++)
  43.         for (j = i+1; j < n; j++)
  44.         {
  45.             cout << "Вес " << i + 1 << "<->" << j + 1 << " = ";
  46.             cin >> w;
  47.             if (w != 0)
  48.             {
  49.                 edge[e].v = i;
  50.                 edge[e].u = j;
  51.                 edge[e].w = w;
  52.                 e++;
  53.                 edge[e].v = j;
  54.                 edge[e].u = i;
  55.                 edge[e].w = w;
  56.                 e++;
  57.             }
  58.         }
  59.  
  60.     cout << "Стартовая вершина: "; cin >> start;
  61.     cout << "Список кратчайших путей:\n";
  62.     bellman_ford(n, start - 1);
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment