Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. std::vector <int> vect(1, 0);
  6. int summa = 0;
  7.  
  8. struct point{
  9. double x;
  10. double y;
  11. point(int a, int b) {
  12. x = a;
  13. y = b;
  14. }
  15. };
  16.  
  17. double dist(point a, point b) {
  18. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  19. }
  20.  
  21. void greedy(std::vector <std::vector <double>>& matrix, std::vector <bool>& used, int node) {
  22. used[node] = true;
  23. double sum = 9999999991;
  24. int mus;
  25. for (int i = 0; i < matrix.size(); ++i) {
  26. if (!used[i] && sum > matrix[node][i]) {
  27. sum = matrix[node][i];
  28. mus = i;
  29. }
  30. }
  31. if (sum != 9999999991) {
  32. summa += sum;
  33. vect.push_back(mus);
  34. greedy(matrix, used, mus);
  35. }
  36. }
  37.  
  38. void transport(int a, int b) {
  39. std::vector <int> vecd(0);
  40. for (int i = 0; i < a; ++i) vecd.push_back(vect[i]);
  41. for (int i = b; i >= a; --i) vecd.push_back(vect[i]);
  42. for (int i = b + 1; i < vect.size(); ++i) vecd.push_back(vect[i]);
  43. vect = vecd;
  44. }
  45.  
  46. int main() {
  47. int n;
  48. std::vector <point> vec;
  49. std::cin >> n;
  50. std::vector <std::vector <double>> matrix(n);
  51. std::vector <bool> used(n);
  52. for (int i = 0; i < n; ++i) {
  53. int a, b;
  54. std::cin >> a >> b;
  55. point p(a, b);
  56. vec.push_back(p);
  57. }
  58. for (int i = 0; i < n; ++i) {
  59. for (int j = 0; j < n; ++j) {
  60. matrix[i].push_back(dist(vec[i], vec[j]));
  61. }
  62. }
  63. greedy(matrix, used, 0);
  64. vect.push_back(0);
  65. int g = 250;
  66. for (int i = 1; i < vect.size() - 2; ++i) {
  67. for (int j = i + 1; j < vect.size() - 1; ++j) {
  68. double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
  69. double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
  70. if (v1 > v2 && g > 0) {
  71. transport(i, j);
  72. i = 1;
  73. j = 2;
  74. --g;
  75. }
  76. }
  77. }
  78. int itog;
  79. int mesto = 99999999;
  80. for (int i = 1; i < vect.size() - 2; ++i) {
  81. double h1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[i + 1]][vect[i + 2]];
  82. double h2 = matrix[vect[i - 1]][vect[i + 1]] + matrix[vect[i]][vect[i + 2]];
  83. if (mesto > h2 - h1) {
  84. mesto = h2 - h1;
  85. itog = i;
  86. }
  87. }
  88. if (n > 230) {
  89. transport(itog, itog + 1);
  90. g = 20;
  91. for (int i = 1; i < vect.size() - 2; ++i) {
  92. for (int j = i + 1; j < vect.size() - 1; ++j) {
  93. double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
  94. double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
  95. if (v1 > v2 && g > 0) {
  96. transport(i, j);
  97. i = 1;
  98. j = 2;
  99. --g;
  100. }
  101. }
  102. }
  103. }
  104. mesto = 999999;
  105. for (int i = 1; i < vect.size() - 2; ++i) {
  106. double h1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[i + 1]][vect[i + 2]];
  107. double h2 = matrix[vect[i - 1]][vect[i + 1]] + matrix[vect[i]][vect[i + 2]];
  108. if (mesto > h2 - h1) {
  109. mesto = h2 - h1;
  110. itog = i;
  111. }
  112. }
  113. if (n > 230) {
  114. transport(itog, itog + 1);
  115. g = 20;
  116. for (int i = 1; i < vect.size() - 2; ++i) {
  117. for (int j = i + 1; j < vect.size() - 1; ++j) {
  118. double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
  119. double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
  120. if (v1 > v2 && g > 0) {
  121. transport(i, j);
  122. i = 1;
  123. j = 2;
  124. --g;
  125. }
  126. }
  127. }
  128. }
  129. for (int i = 0; i < vect.size(); ++i) {
  130. std::cout << vect[i] + 1 << " ";
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement