allia

форд

Jan 7th, 2021 (edited)
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Graph
  5. {
  6.   private:
  7.   int n, **arr, **vse_rebra, shet_reber, *ves, *path;
  8.  
  9.   public:
  10.   Graph (int **matrix, int x, int shet, int *w)
  11.   {
  12.     n = x;
  13.     vse_rebra = matrix;
  14.     shet_reber = shet;
  15.     ves = w;
  16.   }
  17.   void algorithm_ford();
  18.   void get_result_ford();
  19. };
  20.  
  21. void Graph::algorithm_ford()
  22. {
  23.  path = new int[n];
  24.  
  25.  path[0] = 0;
  26.  
  27.  for (int i = 1; i < n; i++)
  28.    path[i] = 1000;
  29.  
  30.  for (int i = 0; i < n-1; i++)
  31.   for (int j = 0; j < shet_reber; j++)
  32.    if (path[vse_rebra[j][0]] < 1000)
  33.     if (path[vse_rebra[j][0]] + ves[j] < path[vse_rebra[j][1]])
  34.     path[vse_rebra[j][1]] = path[vse_rebra[j][0]] + ves[j];
  35. }
  36.  
  37. void Graph::get_result_ford()
  38. {
  39.   for (int i = 0; i < n; i++)
  40.   {
  41.     if (path[i] == 1000)
  42.      cout << "No" << " ";
  43.     else
  44.      cout << path[i] << " ";
  45.   }
  46. }
  47.  
  48. int main()
  49. {
  50.  int n, shet_reber;
  51.  cin >> n >> shet_reber;
  52.  
  53. int **arr = new int*[shet_reber];
  54. int *w = new int[shet_reber];
  55.  
  56. for ( int i = 0; i < shet_reber; i++)
  57.      arr[i] = new int[2];
  58.    
  59. for (int i = 0; i < shet_reber; i++)
  60.   {
  61.     for (int j = 0; j < 2; j++)  
  62.          {
  63.            cin >> arr[i][j];
  64.            arr[i][j]--;
  65.          }
  66.     cin >> w[i];
  67.   }
  68.  
  69.  Graph object(arr, n, shet_reber, w);
  70.  object.algorithm_ford();
  71.  object.get_result_ford();
  72. }
Add Comment
Please, Sign In to add comment