Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- struct Coord{
- float x;
- float y;
- };
- float dist(Coord a, Coord b) {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- int main() {
- int n;
- cin >> n;
- vector<bool> used(n, false);
- vector<Coord> g(n);
- for (size_t i = 0; i != n; ++i) {
- cin >> g[i].x >> g[i].y;
- }
- vector<vector<float>> G(n, vector<float>(n));
- for (size_t i = 0; i != n - 1; ++i) {
- for (size_t j = i + 1; j != n; ++ j) {
- G[i][j] = dist(g[i], g[j]);
- G[j][i] = dist(g[i], g[j]);
- }
- }
- vector<int> answer;
- answer.push_back(0);
- used[0] = true;
- for (size_t i = 1; i != n; ++i) {
- float min_length = 2 * 1e18;
- int ans;
- for (size_t j = 0; j != n; ++j) {
- if (G[answer[i - 1]][j] < min_length && answer[i - 1] != j && !used[j]) {
- min_length = G[answer[i - 1]][j];
- ans = j;
- }
- }
- used[ans] = true;
- answer.push_back(ans);
- }
- answer.push_back(0);
- for (size_t i = 0; i != n + 1; ++i) {
- cout << answer[i] + 1 << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement