Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fin cin
  6. #define fout cout
  7. //ifstream fin("x.in"); ofstream fout("x.out");
  8.  
  9. const int nmax = 500;
  10.  
  11. int n;
  12. typedef pair<double, double> pct;
  13.  
  14. int indice[nmax + 1];
  15. pct v[nmax + 1];
  16.  
  17. bool fol[nmax + 1];
  18.  
  19. inline double dist (pct a, pct b) {
  20.     return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
  21. }
  22.  
  23. pair<double, vector<int>> solve () {
  24.     int s = rand() % n + 1;
  25.  
  26.     //fout << s <<"\n";
  27.     for (int i = 1; i <= n; ++ i)
  28.         fol[i] = 0;
  29.  
  30.     int x = s;
  31.     double sum = 0;
  32.     vector<int> ord;
  33.     ord.push_back(s);
  34.  
  35.     for (int i = 2; i <= n; ++ i) {
  36.         fol[x] = 1;
  37.         int ind = -1;
  38.         double mndist = INFINITY;
  39.  
  40.         for (int j = 1; j <= n; j ++) {
  41.             if (fol[j]) continue;
  42.  
  43.             double aux = dist(v[x], v[j]);
  44.             if (aux < mndist) {
  45.                 mndist = aux;
  46.                 ind = j;
  47.             }
  48.         }
  49.  
  50.         sum += mndist;
  51.         x = ind;
  52.         ord.push_back(x);
  53.     }
  54.  
  55.     sum += dist(v[s], v[x]);
  56.     ord.push_back(s);
  57.  
  58.     return {sum, ord};
  59. }
  60.  
  61. int main () {
  62.     fin >> n;
  63.  
  64.     for (int i = 1; i <= n; ++ i) {
  65.         int x;
  66.         fin >> x; indice[i] = x;
  67.         fin >> v[i].first >> v[i].second;
  68.     }
  69.  
  70.     double cst = clock();
  71.  
  72.     double best = INFINITY;
  73.     vector<int> ans;
  74.     srand(time(0));
  75.  
  76.     for (int pas = 0; ; ++ pas) {
  77.         if (pas % 10 == 0 && (clock() - cst) / CLOCKS_PER_SEC > 4.8) {
  78.             break;
  79.         }
  80.  
  81.         pair<double, vector<int>> ans2 = solve();
  82.         if (ans2.first < best) {
  83.             best = ans2.first;
  84.             ans = ans2.second;
  85.         }
  86.     }
  87.  
  88.     //fout << best << "\n";
  89.  
  90.     for (auto i : ans)
  91.         fout << indice[i] << " ";
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement