Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream> // подключаем библиотеку
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <ctime> // содержит time()
  7. #include <cmath>
  8. #include <numeric>
  9. #include <random>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. float Euklidus(int i, int j, vector<pair<int, int> >& V) {
  15.     float temp = sqrt(pow(V[i].first - V[j].first, 2) + pow(V[i].second - V[j].second, 2));
  16.     return temp;
  17. }
  18.  
  19. void f(int i, int j, float& answer, vector<vector<float> >& M, vector<int>& way) {
  20.     int n = way.size();
  21.     float answer_minus = 0;
  22.     float answer_plus = 0;
  23.     if (abs(i - j) > 1) {
  24.         answer_minus = M[way[i-1]][way[i]] + M[way[i]][way[i+1]] + M[way[j]][way[j-1]] + M[way[j]][way[j+1]];
  25.         answer_plus = M[way[i-1]][way[j]] + M[way[j]][way[i+1]] + M[way[j-1]][way[i]] + M[way[i]][way[j+1]];
  26.     } else if (abs(i - j) == 1) {
  27.         answer_minus = M[way[i-1]][way[i]] + M[way[j]][way[j+1]];
  28.         answer_plus = M[way[i-1]][way[j]] + M[way[i]][way[j+1]];
  29.     }
  30.     if (answer_plus < answer_minus) {
  31.         swap(way[i], way[j]);
  32.         answer += (answer_plus - answer_minus);
  33.     } else {
  34.         ;
  35.     }
  36. }
  37.  
  38. void g(int i, int j, float& answer, vector<vector<float> >& M, vector<int>& way) { // i < j != 0
  39.     int n = way.size();
  40.     float answer_minus = 0;
  41.     float answer_plus = 0;
  42.     if (abs(i - j) > 0) {
  43.         answer_minus = M[way[i+1]][way[i]] + M[way[j]][way[j-1]];
  44.         answer_plus = M[way[i]][way[j-1]] + M[way[i+1]][way[j]];
  45.         if (true) {
  46.             for (int t = 1; t < ((j-i-1) / 2) + 1; ++t) {
  47.                 swap(way[i+t], way[j-t]);
  48.             }
  49.             answer += (answer_plus - answer_minus);
  50.         }
  51.     }
  52.     /* else if (abs(i - j) == 1) {
  53.         answer_minus = M[way[i]][way[i+1]] + M[way[(j+1) % n]][way[j]] + M[way[i+1]][way[j]];
  54.         answer_plus = M[way[i]][way[j]] + M[way[i]][way[j+1]];
  55.     }
  56.     if (answer_plus < answer_minus) {
  57.         for (int t = 1; t < ((j-i-1) / 2) + 1; ++t) {
  58.             swap(way[i+t], way[j-t]);
  59.         }
  60.         answer += (answer_plus - answer_minus);
  61.     } else {
  62.         ;
  63.     }*/
  64. }
  65.  
  66. int main() {
  67.     /*ifstream file; // создаем объект класса ifstream
  68.     file.open("..\\input.txt"); // открываем файл
  69.     int n;
  70.     file >> n;*/
  71.     int n;
  72.     cin >> n;
  73.     if (n == 0) {
  74.         return 0;
  75.     } else if (n == 1) {
  76.         cout << 0 << endl;
  77.         return 0;
  78.     } else if (n == 2) {
  79.         cout << 0 << ' ' << 1 << ' ' << 0 << endl;
  80.         return 0;
  81.     }
  82.     int R = 500000;
  83.     int randomDigits[R] = {};
  84.     srand(time(NULL));
  85.     for (int i = 0; i < R; i++) {
  86.         randomDigits[i] = rand() % n; // n-1 ????
  87.         if ((randomDigits[i]) == 0) {
  88.             randomDigits[i]++;
  89.         }
  90.     }
  91.     pair<int, int> p;
  92.     vector<pair<int, int> > V(n);
  93.     vector<vector<float> > M(n, vector<float>(n, 0));
  94.     for (int i = 0; i < n; ++i) {
  95.         cin >> p.first >> p.second;
  96.         //file >> p.first >> p.second;
  97.         V[i] = p;
  98.     }
  99.     float t;
  100.     for (int i = 0; i < n-1; ++i) {
  101.         for (int j = i+1; j < n; ++j) {
  102.             t = Euklidus(i, j, V);
  103.             M[i][j] = t;
  104.             M[j][i] = t;
  105.         }
  106.     }
  107.     for (int i = 0; i < n; ++i) {
  108.         for (int j = 0; j < n; ++j) {
  109.             cout << M[i][j] << ' ';
  110.         }
  111.         cout << endl;
  112.     }
  113.     vector<bool> color(n);
  114.     int colored = 1;
  115.     color[0] = true;
  116.     int current_vertex = 0;
  117.     vector<int> way(n);
  118.     //vector<float> dist_to_previous(n);
  119.     way[0] = 0;
  120.     float all_dist = 0;
  121.     for (int k = 1; k < n; ++k) {
  122.         int vertex_min_dist;
  123.         float min_dist = 1000000;
  124.         for (int i = 0; i < n; ++i) {
  125.             if (!color[i]) {
  126.                 if (M[current_vertex][i] < min_dist && i != current_vertex) {
  127.                     min_dist = M[current_vertex][i];
  128.                     vertex_min_dist = i;
  129.                 }
  130.             }
  131.         }
  132.         cout << endl;
  133.         cout << "cur vertex:= " << vertex_min_dist <<  "   min_dist = " << min_dist << endl;
  134.         color[vertex_min_dist] = true;
  135.         way[k] = vertex_min_dist;
  136.         current_vertex = vertex_min_dist;
  137.         for (int l = 0; l < n; ++l) {
  138.             cout << color[l] << ' ';
  139.         }
  140.         cout << endl;
  141.     }
  142.     way.push_back(0);
  143.  
  144.     float answer = 0;
  145.     for (int i = 0; i < n-1; ++i) {
  146.         answer += M[way[i]][way[i+1]];
  147.     }
  148.     answer += M[way[n-1]][way[0]];
  149.     cout << "answer before = " << answer << endl;
  150.     for (int i = 0; i < n; ++i) {
  151.         cout << way[i]+1 << ' ';
  152.     }
  153.     int counter = 0;
  154.     int left;
  155.     int right;
  156.     for (int i = 1; i < R-1; ++i) {
  157.         left = min(randomDigits[i], randomDigits[(i+1) % n]);
  158.         right = max(randomDigits[i], randomDigits[(i+1) % n]);
  159.         f(left, right, answer, M, way);
  160.         ++counter;
  161.     }
  162.     cout << way[0]+1 << endl;
  163.  
  164.     cout << "answer after = " << answer << endl;
  165.     for (int i = 0; i < n; ++i) {
  166.         cout << way[i]+1 << ' ';
  167.     }
  168.     cout << way[0]+1 << ' ' << counter << endl;
  169.  
  170.  
  171.     g(1, 4, answer, M, way);
  172.  
  173.     cout << "answer after g = " << answer << endl;
  174.     for (int i = 0; i < n; ++i) {
  175.         cout << way[i]+1 << ' ';
  176.     }
  177.     cout << way[0]+1 << ' ' << counter << endl;
  178.    
  179.     vector<int> permut(n);
  180.     generate(permut.begin(), permut.end(), [t = 0] () mutable { return t++; });
  181.     random_device rd;
  182.     mt19937 g(rd());
  183.     shuffle(permut.begin()+1, permut.end(), g);
  184.     permut.push_back(permut[0]);
  185.     for (auto elem : permut) {
  186.         cout << elem << ' ';
  187.     }
  188.     cout << endl;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement