Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7.  
  8. using std::cin;
  9. using std::cout;
  10. using Matrix = std::vector<std::vector<double>>;
  11.  
  12.  
  13.  
  14. double dist(std::pair<double, double> vertex1, std::pair<double, double> vertex2) {
  15.     return sqrt((vertex1.first - vertex2.first) * (vertex1.first - vertex2.first)
  16.         + (vertex1.second - vertex2.second) * (vertex1.second - vertex2.second));
  17. }
  18.  
  19.  
  20.  
  21. std::vector<size_t> local_search(const Matrix& graph) {
  22.     std::vector<size_t> path(graph.size() + 1);
  23.     path[0] = 0;
  24.     path[graph.size()] = 0;
  25.     size_t cur = 0;
  26.     std::vector<bool> visited(graph.size());
  27.     visited[0] = true;
  28.     double min = -1;
  29.     for (size_t i = 1; i != graph.size(); ++i) {
  30.         for (size_t j = 0; j != graph.size(); ++j) {
  31.             if (j != path[i - 1]) {
  32.                 if ((min == -1 || graph[path[i - 1]][j] < min) && !visited[j]) {
  33.                     path[i] = j;
  34.                     min = graph[path[i - 1]][j];
  35.                 }
  36.             }
  37.         }
  38.         visited[path[i]] = true;
  39.         min = -1;
  40.     }
  41.  
  42.     for (size_t k = 0; k != 10; ++k) {
  43.         for (size_t i = 1; i != graph.size() - 1; ++i) {
  44.             double difference = graph[path[i - 1]][path[i + 1]] + graph[path[i]][path[i + 2]]
  45.                 - graph[path[i - 1]][path[i]] - graph[path[i + 1]][path[i + 2]];
  46.  
  47.             // cout << "path num " << i << " diff" << difference << "\n";
  48.             if (difference < 0) {
  49.                 std::swap(path[i], path[i + 1]);
  50.             }
  51.         }
  52.     }
  53.  
  54.     for (size_t k = 0; k != 15; ++k) {
  55.         for (size_t i = 1; i != graph.size() - 1; ++i) {
  56.             for (size_t j = 1; j != graph.size() - 1; ++j) {
  57.                 if (i != j) {
  58.                     double difference =
  59.                         graph[path[i - 1]][path[j - 1]] + graph[path[i]][path[j]] -
  60.                         graph[path[i - 1]][path[i]] - graph[path[j - 1]][path[j]];
  61.  
  62.                     if (difference < 0) {
  63.                         std::reverse(path.begin() + i, path.begin() + j);
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     return path;
  71. }
  72.  
  73.  
  74. int main() {
  75.     size_t vertex_num;
  76.     cin >> vertex_num;
  77.  
  78.     std::vector<std::pair<double, double>> vertexes(vertex_num);
  79.  
  80.     for (size_t i = 0; i != vertex_num; ++i) {
  81.         int first, second;
  82.         cin >> first >> second;
  83.         vertexes[i] = std::make_pair(first, second);
  84.     }
  85.  
  86.     if (vertex_num == 1) {
  87.         cout << "1\n";
  88.         return 0;
  89.     } else if (vertex_num == 2) {
  90.         cout << "1 2 1\n";
  91.         return 0;
  92.     }
  93.  
  94.  
  95.     Matrix graph(vertex_num,
  96.         std::vector<double>(vertex_num));
  97.  
  98.     for (size_t i = 0; i != vertex_num; ++i) {
  99.         for (size_t j = 0; j != vertex_num; ++j) {
  100.             graph[i][j] = dist(vertexes[i], vertexes[j]);
  101.         }
  102.     }
  103.  
  104.     auto result = local_search(graph);
  105.     for (const auto& elem : result) {
  106.         cout << elem + 1 << " ";
  107.     }
  108.     cout << std::endl;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement