Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void travel(const vector<vector<double>>& graph, vector<int>& path, double& l) {
  12.     int f, f1, s, s1, flag = 0;
  13.     int n = graph.size();
  14.     double tmp;
  15.     for (size_t i = 0; i <= n - 3; ++i) {
  16.         for (size_t j = i + 2; j <= n - 1; ++j) {
  17.             if (i != 0 || j != n - 1) {
  18.                 f = path[i];
  19.                 f1 = path[i + 1];
  20.                 s = path[j];
  21.                 s1 = path[(j + 1) % n];
  22.                 /* for (auto i : ptr) {
  23.                 cout << i << " ";
  24.                 }
  25.                 cout << endl; */
  26.                 tmp = l - graph[f][f1] - graph[s][s1] + graph[f][s] + graph[f1][s1];
  27.                 if (tmp < l) {
  28.                     l = tmp;
  29.                     reverse(path.begin() + i + 1, path.begin() + j + 1);
  30.                     flag = 1;
  31.                 }
  32.             }
  33.         }
  34.     }
  35.     if (flag == 1) {
  36.         travel(graph, path, l);
  37.     }
  38. }
  39.  
  40. int main() {
  41.     std::srand(unsigned(std::time(0)));
  42.     int n;
  43.     cin >> n;
  44.     vector<pair<int, int>> cities;
  45.     for (size_t i = 0; i < n; ++i) {
  46.         int x, y;
  47.         cin >> x >> y;
  48.         cities.push_back(make_pair(x, y));
  49.     }
  50.     vector<vector<double>> graph(n);
  51.     for (size_t i = 0; i < n; ++i) {
  52.         graph[i].resize(n);
  53.     }
  54.     if (n == 1) {
  55.         cout << 1;
  56.         return 0;
  57.     }
  58.     if (n == 2) {
  59.         cout << " 1 2 1";
  60.         return 0;
  61.     }
  62.     for (size_t i = 0; i < n; ++i) {
  63.         for (size_t j = 0; j < n; ++j) {
  64.             if (i != j) {
  65.                 graph[i][j] =
  66.                     sqrt((cities[i].first - cities[j].first) *
  67.                     (cities[i].first - cities[j].first) +
  68.                     (cities[i].second - cities[j].second) *
  69.                     (cities[i].second - cities[j].second));
  70.             }
  71.         }
  72.     }
  73.     vector<int> path(n), best, visited(n);
  74.     double l = 0, best_l;
  75.     best.push_back(0);
  76.     visited[0] = 1;
  77.     for (size_t i = 1; i <= n; ++i) {
  78.         float tmp = 100000000000;
  79.         int ptr;
  80.         ptr = -1;
  81.         for (size_t j = 1; j < n; ++j) {
  82.             if (visited[j] != 1 && best[i - 1] != j) {
  83.                 if (graph[best[i - 1]][j] < tmp) {
  84.                     ptr = j;
  85.                     tmp = graph[best[i - 1]][j];
  86.                 }
  87.             }
  88.         }
  89.         if (ptr == -1) {
  90.         } else {
  91.             best.push_back(ptr);
  92.             visited[ptr] = 1;
  93.         }
  94.     }
  95.     best.push_back(0);
  96.     for (size_t i = 0; i < n; ++i) {
  97.         l += graph[best[i]][best[(i + 1) % n]];
  98.     }
  99.     travel(graph, best, l);
  100.     path = best;
  101.     best_l = l;
  102.     random_shuffle(best.begin() + 1, best.end() - 1);
  103.     for (size_t k = 0; k <= 20; ++k) {
  104.         random_shuffle(best.begin() + 1, best.end() - 1);
  105.         l = 0;
  106.         for (size_t i = 0; i < n; ++i) {
  107.             l += graph[best[i]][best[(i + 1) % n]];
  108.         }
  109.         travel(graph, best, l);
  110.         if (l < best_l) {
  111.             path = best;
  112.             random_shuffle(best.begin() + 1, best.end() - 1);
  113.         }
  114.     }
  115.     for (auto i : path) {
  116.         cout << i + 2 - 1 << " ";
  117.     }
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement