Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <random>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12. size_t n;
  13. cin >> n;
  14. clock_t time = clock();
  15. set<int> not_visit;
  16. vector<int> res;
  17. vector<vector<float>> map(n, vector<float>(n));
  18. vector<pair<float, float>> cities;
  19. cities.resize(n);
  20. int nt = 0;
  21. int c = 0;
  22. int cv = 0;
  23. float currl = 0.0;
  24. for(int i = 0; i != n; ++i) {
  25. cin >> cities[i].first >> cities[i].second;
  26. not_visit.insert(i);
  27. }
  28. for(int i = 0; i < n; ++i) {
  29. for (int j = i + 1; j < n ; ++j) {
  30. float d = sqrt(pow(cities[i].first - cities[j].first, 2) +
  31. pow(cities[i].second - cities[j].second, 2));
  32. map[i][j] = d;
  33. map[j][i] = d;
  34. }
  35. }
  36. while(!not_visit.empty()) {
  37. not_visit.erase(cv);
  38. res.push_back(cv);
  39. c = 0;
  40. for (const auto elem : not_visit) {
  41. if ((c != 0) && (map[elem][cv] < currl)) {
  42. currl = map[elem][cv];
  43. nt = elem;
  44. } else if (c == 0) {
  45. currl = map[elem][cv];
  46. nt = elem;
  47. }
  48. c += 1;
  49. }
  50. cv = nt;
  51. }
  52. res.push_back(0);
  53.  
  54. while (((clock() - time)/(float)CLOCKS_PER_SEC) <= 9.99) {
  55. random_device randd;
  56. uniform_int_distribution<int> id(1, n - 2);
  57. mt19937 mt19_(randd());
  58. int i = id(mt19_);
  59. uniform_int_distribution<int> id_sec(i + 1, n + 1 - 2);
  60. int j = id_sec(mt19_);
  61. float new_ = map[res[j]][res[i - 1]] + map[res[i]][res[j + 1]];
  62. float c = map[res[j]][res[j + 1]] + map[res[i]][res[i - 1]];
  63.  
  64. if(new_ < c) {
  65. reverse(res.begin() + i, res.begin() + j + 1);
  66. }
  67. }
  68. for (size_t i = 0; i != res.size(); ++i) {
  69. cout << res[i] + 1 << ' ';
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement