Advertisement
semenrbt

Untitled

Nov 20th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. class route {
  8.     int *r, n; // r - массив, представляющий маршрут; n - кол-во городов
  9. public:
  10.     route(int nn) {
  11.         n = nn;
  12.         r = new int[nn];
  13.         for (int i = 0; i < n; i++) r[i] = i;
  14.     }
  15.  
  16.     route(const route& c) {
  17.         n = c.n;
  18.         r = new int[c.n];
  19.         for (int i = 0; i < n; i++) r[i] = i;
  20.     }
  21.  
  22.     route& operator = (const route& a) {
  23.         if (this != &a) {
  24.             delete[] a;
  25.             n = a.n;
  26.             r = new int[a];
  27.             for (int i = 0; i < n; i++) r[i] = i;
  28.         }
  29.         return *this;
  30.     }
  31.  
  32.     ~route() {
  33.         if (r != NULL) delete[] r;
  34.         r = NULL;
  35.     }
  36.  
  37.     int route_price(int** P) {
  38.         if (P == NULL) return -1;
  39.         int i = 0, S = 0;
  40.         for (i = 0; i < n - 1; i++) {
  41.             S = S + P[r[i]][r[i + 1]];
  42.         }
  43.         S = S + P[r[i]][r[0]];
  44.         return S;
  45.     }
  46. };
  47.  
  48. int** MakeArray(int N) {
  49.     if (N <= 0) return NULL;
  50.     int** ptr = new int* [N];
  51.     for (int i = 0; i < N; i++) {
  52.         ptr[i] = new int* [N];
  53.         for (int j = 0; j < N; j++) {
  54.             if (i == j) ptr[i][j] = 0;
  55.             else ptr[i][j] = 1 + rand() % 9;
  56.         }
  57.     }
  58.     return ptr;
  59. }
  60.  
  61.  
  62.  
  63. int main() {
  64.     srand(time(NULL));
  65.     int min = INT_MAX;
  66.     int n = 12;
  67.     int** P = MakeArray(n);
  68.  
  69.     route i(n), j(n);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement