Advertisement
AmirVagapov

Untitled

Mar 8th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 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.     sum +=arr[r[n-1]][r[0]];
  42.     return sum;  
  43.    
  44. }
  45. bool route::next_route() {
  46.     int i = 0;
  47.     int j;
  48.     for (int k = n-2; k >= 1; k--)
  49.         if(r[k]<r[k+1]) {
  50.         i = k;
  51.         break;    
  52.         }
  53.     if (!(i)) return false;
  54.     for (int k = n-1; k >= i+1; k--)
  55.         if(r[i]<r[k]) {
  56.             j = k;
  57.             break;
  58.         }
  59.         swap(r[i],r[j]);
  60.     for (int low = i+1, high = n-1;low<high; low++,high--)
  61.             swap(r[low],r[high]);
  62.     return true;
  63. }
  64. ostream & operator << (ostream& out, const route& a) {
  65.     for (int i = 0; i < a.n; i++) {
  66.         out<<a.r[i]<<"\t";
  67.     }
  68.     return out;
  69. }
  70.  
  71. int main() {
  72.     int n;
  73.     cin>>n;
  74.     route d(n);
  75.    
  76.     int **cost = new int*[n];
  77.     for (int i = 0; i < n; i++) {
  78.         cost[i] = new int[n];
  79.         for (int j = 0; j < n; j++) {
  80.             cin>>cost[i][j];
  81.         }
  82.     }
  83.     route temp = d;
  84.     int min = d.route_price(cost);
  85.      cout<<temp<<"\t"<<temp.route_price(cost)<<endl;
  86.     while(temp.next_route()) {
  87.          cout<<temp<<"\t"<<temp.route_price(cost)<<endl;
  88.         int tmin = temp.route_price(cost);
  89.         if (tmin<min) {
  90.             d = temp;
  91.             min = tmin;
  92.         }
  93.     }
  94.     cout<<min<<endl<<d;
  95.     for (int i = 0; i < n; i++) {
  96.         delete [] cost[i];
  97.     }
  98.     delete [] cost;
  99.     return 0;
  100. }
  101. /*
  102. 5
  103. 0 3 1 3 4
  104. 5 0 3 2 4
  105. 4 4 0 2 4
  106. 4 8 2 0 4
  107. 2 8 7 1 0
  108.  
  109. 13
  110. 0   1   3   2   4
  111. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement