Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Coord{
  8.     float x;
  9.     float y;
  10. };
  11.  
  12. float dist(Coord a, Coord b) {
  13.     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  14. }
  15.  
  16. int main() {
  17.     int n;
  18.     cin >> n;
  19.     vector<bool> used(n, false);
  20.     vector<Coord> g(n);
  21.     for (size_t i = 0; i != n; ++i) {
  22.         cin >> g[i].x >> g[i].y;
  23.     }
  24.     vector<vector<float>> G(n, vector<float>(n));
  25.     for (size_t i = 0; i != n - 1; ++i) {
  26.         for (size_t j = i + 1; j != n; ++ j) {
  27.             G[i][j] = dist(g[i], g[j]);
  28.             G[j][i] = dist(g[i], g[j]);
  29.         }
  30.     }
  31.     vector<int> answer;
  32.     answer.push_back(0);
  33.     used[0] = true;
  34.     for (size_t i = 1; i != n; ++i) {
  35.         float min_length = 2 * 1e18;
  36.         int ans;
  37.         for (size_t j = 0; j != n; ++j) {
  38.             if (G[answer[i - 1]][j] < min_length && answer[i - 1] != j && !used[j]) {
  39.                 min_length = G[answer[i - 1]][j];
  40.                 ans = j;
  41.             }
  42.         }
  43.         used[ans] = true;
  44.         answer.push_back(ans);
  45.     }
  46.     answer.push_back(0);
  47.     for (size_t i = 0; i != n + 1; ++i) {
  48.         cout << answer[i] + 1 << ' ';
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement