allia

отрицательные циклы

Jan 8th, 2021 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct rebro
  6. {
  7.  int begin, end, ves;
  8.  void new_rebro(int begin, int end, int ves)
  9.  {
  10.    this->begin = begin;
  11.    this->end = end;
  12.    this->ves = ves;
  13.  }
  14. };
  15.  
  16. class Graph
  17. {
  18.   private:
  19.   int n, **arr, shet_reber, *path;
  20.   rebro *vse_rebra;
  21.  
  22.   public:
  23.   Graph (int **matrix, int x, int shet)
  24.   {
  25.     n = x;
  26.     arr = matrix;
  27.     shet_reber = shet;
  28.  
  29.     vse_rebra = new rebro[shet_reber];
  30.     spisok();
  31.   }
  32.   void spisok();
  33.   void poisk_cycles();
  34. };
  35.  
  36. void Graph::spisok()
  37. {
  38.   int k = 0;
  39.  
  40.   for (int i = 0; i < n; i++)
  41.    for (int j = 0; j < n; j++)
  42.     if (arr[i][j] != 100000)
  43.     {
  44.       vse_rebra[k].new_rebro(i, j, arr[i][j]);
  45.       k++;
  46.     }
  47. }
  48.  
  49. void Graph::poisk_cycles()
  50. {
  51.  path = new int[n];
  52.  int *d = new int[n];
  53.  int x;
  54.  
  55.  for (int i = 0; i < n; i++)
  56.    {
  57.      path[i] = -1;
  58.      d[i] = 0;
  59.    }
  60.  
  61.     for (int i = 0; i < n; ++i)
  62.     {
  63.      x = -1;
  64.     for (int j = 0; j < shet_reber; ++j)
  65.  
  66.      if (d[vse_rebra[j].begin] > d[vse_rebra[j].end] + vse_rebra[j].ves)
  67.       {
  68.         d[vse_rebra[j].begin] = max (-1000000, d[vse_rebra[j].end] + vse_rebra[j].ves);
  69.         path[vse_rebra[j].begin] = vse_rebra[j].end;
  70.         x = vse_rebra[j].begin;
  71.       }
  72.   }
  73.    
  74.   if (x == -1)
  75.     cout << "NO";
  76.   else
  77.   {
  78.     cout << "YES" << endl;
  79.     vector<int> cycle;
  80.    
  81.     int y = x;
  82.         for (int i = 0; i < n; ++i)
  83.             y = path[y];
  84.  
  85.         for (int cur = x; ; cur = path[cur])
  86.         {
  87.             cycle.push_back (cur);
  88.             if (cur == y && cycle.size() > 1)  
  89.              break;
  90.         }
  91.     cout << cycle.size() << endl;
  92.     for (int i = 0; i < cycle.size(); ++i)
  93.       cout << cycle[i]+1 << " ";
  94.   }
  95. }
  96.  
  97. int main()
  98. {
  99.  int n, shet_reber = 0;
  100.  cin >> n;
  101.  
  102. int **arr = new int*[n];
  103.  
  104. for ( int i = 0; i < n; i++)
  105.      arr[i] = new int[n];
  106.    
  107. for (int i = 0; i < n; i++)
  108.   for (int j = 0; j < n; j++)  
  109.       {
  110.         cin >> arr[i][j];
  111.         if (arr[i][j] != 100000)
  112.          shet_reber++;
  113.       }
  114.  
  115.  Graph object(arr, n, shet_reber);
  116.  object.poisk_cycles();
  117. }
  118. 6
  119. 0 -50 0 -34 0 0
  120. 0 0 -45 0 0 10
  121. -28 0 0 0 0 0
  122. 0 0 -31 0 25 0
  123. 48 0 0 0 0 60
  124. 20 0 0 0 0 0
Add Comment
Please, Sign In to add comment