Advertisement
AmirVagapov

Untitled

Feb 15th, 2022
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5. // Класс Маршрут
  6. class route {  
  7.     int *r, n;    // r – массив, представляющий маршрут; n – количество городов
  8.     public:  
  9.     route(int num = 0) {
  10.         n = num;
  11.         r = new int[n];
  12.         for (int i = 0; i < n; i++)
  13.             r[i] = i;
  14.     }
  15.     route(const route&a) {
  16.         n = a.n;
  17.         r = new int[n];
  18.         for (int i = 0; i < n; i++)
  19.             r[i]=a.r[i];
  20.     }    // конструктор копирования
  21.     route & operator = (const route&a) {
  22.         if (this != &a) {
  23.             n = a.n;
  24.             delete []r;
  25.             r = new int[n];
  26.             for (int i = 0; i < n; i++)
  27.                 r[i]=a.r[i];
  28.         }
  29.         return *this;
  30.     }   // операция присваивания
  31.     ~route() { if(r) delete []r; r = NULL; } // деструктор
  32.     int route_price(int **);  // вычисляет стоимость маршрута по матрице стоимости
  33.     bool next_route();   // вычисляет следующий маршрут, используя алгоритм Дейкстры
  34.     friend ostream & operator << (ostream&, const route&); // вывод маршрута
  35. };
  36.  
  37. int route::route_price(int **arr) {
  38.     int sum = 0;
  39.     for (int i = 0; i < n; i++)
  40.         sum +=arr[r[i]][r[i+1]];
  41.     return sum;    
  42. }
  43. bool route::next_route() {
  44.     int i = 0;
  45.     int j;
  46.     for (int k = n-2; k >= 1; k--)
  47.         if(r[k]<r[k+1]) {
  48.         i = k;
  49.         break;    
  50.         }
  51.     if (!(i)) return false;
  52.     for (int k = n-1; k >= i+1; k--)
  53.         if(r[i]<r[k]) {
  54.             j = k;
  55.             break;
  56.         }
  57.         swap(r[i],r[j]);
  58.     for (int low = i+1, high = n-1;low<high; low++,high--)
  59.             swap(r[low],r[high]);
  60.     return true;
  61. }
  62. ostream & operator << (ostream& out, const route& a) {
  63.     for (int i = 0; i < a.n; i++) {
  64.         out<<a.r[i]<<"\t";
  65.     }
  66.     return out;
  67. }
  68.  
  69. int main() {
  70.     int n;
  71.     cin>>n;
  72.     route d(n);
  73.     int **cost = new int*[n];
  74.     for (int i = 0; i < n; i++) {
  75.         cost[i] = new int[n];
  76.         for (int j = 0; j < n; j++) {
  77.             cin>>cost[i][j];
  78.         }
  79.     }
  80.     route temp = d;
  81.     int min = d.route_price(cost);
  82.      cout<<temp<<"\t"<<temp.route_price(cost)<<endl;
  83.     while(temp.next_route()) {
  84.          cout<<temp<<"\t"<<temp.route_price(cost)<<endl;
  85.         int tmin = temp.route_price(cost);
  86.         if (tmin<min) {
  87.             d = temp;
  88.             min = tmin;
  89.         }
  90.     }
  91.     cout<<min<<endl<<d;
  92.     for (int i = 0; i < n; i++) {
  93.         delete [] cost[i];
  94.     }
  95.     delete [] cost;
  96.     return 0;
  97. }
  98. /*
  99. 5
  100. 0 3 1 3 4
  101. 5 0 3 2 4
  102. 4 4 0 2 4
  103. 4 8 2 0 4
  104. 2 8 7 1 0
  105.  
  106. 13
  107. 0   1   3   2   4
  108. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement