Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- std::vector <int> vect(1, 0);
- int summa = 0;
- struct point{
- double x;
- double y;
- point(int a, int b) {
- x = a;
- y = b;
- }
- };
- double dist(point a, point b) {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- void greedy(std::vector <std::vector <double>>& matrix, std::vector <bool>& used, int node) {
- used[node] = true;
- double sum = 9999999991;
- int mus;
- for (int i = 0; i < matrix.size(); ++i) {
- if (!used[i] && sum > matrix[node][i]) {
- sum = matrix[node][i];
- mus = i;
- }
- }
- if (sum != 9999999991) {
- summa += sum;
- vect.push_back(mus);
- greedy(matrix, used, mus);
- }
- }
- void transport(int a, int b) {
- std::vector <int> vecd(0);
- for (int i = 0; i < a; ++i) vecd.push_back(vect[i]);
- for (int i = b; i >= a; --i) vecd.push_back(vect[i]);
- for (int i = b + 1; i < vect.size(); ++i) vecd.push_back(vect[i]);
- vect = vecd;
- }
- int main() {
- int n;
- std::vector <point> vec;
- std::cin >> n;
- std::vector <std::vector <double>> matrix(n);
- std::vector <bool> used(n);
- for (int i = 0; i < n; ++i) {
- int a, b;
- std::cin >> a >> b;
- point p(a, b);
- vec.push_back(p);
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- matrix[i].push_back(dist(vec[i], vec[j]));
- }
- }
- greedy(matrix, used, 0);
- vect.push_back(0);
- int g = 250;
- for (int i = 1; i < vect.size() - 2; ++i) {
- for (int j = i + 1; j < vect.size() - 1; ++j) {
- double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
- double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
- if (v1 > v2 && g > 0) {
- transport(i, j);
- i = 1;
- j = 2;
- --g;
- }
- }
- }
- int itog;
- int mesto = 99999999;
- for (int i = 1; i < vect.size() - 2; ++i) {
- double h1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[i + 1]][vect[i + 2]];
- double h2 = matrix[vect[i - 1]][vect[i + 1]] + matrix[vect[i]][vect[i + 2]];
- if (mesto > h2 - h1) {
- mesto = h2 - h1;
- itog = i;
- }
- }
- if (n > 230) {
- transport(itog, itog + 1);
- g = 20;
- for (int i = 1; i < vect.size() - 2; ++i) {
- for (int j = i + 1; j < vect.size() - 1; ++j) {
- double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
- double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
- if (v1 > v2 && g > 0) {
- transport(i, j);
- i = 1;
- j = 2;
- --g;
- }
- }
- }
- }
- mesto = 999999;
- for (int i = 1; i < vect.size() - 2; ++i) {
- double h1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[i + 1]][vect[i + 2]];
- double h2 = matrix[vect[i - 1]][vect[i + 1]] + matrix[vect[i]][vect[i + 2]];
- if (mesto > h2 - h1) {
- mesto = h2 - h1;
- itog = i;
- }
- }
- if (n > 230) {
- transport(itog, itog + 1);
- g = 20;
- for (int i = 1; i < vect.size() - 2; ++i) {
- for (int j = i + 1; j < vect.size() - 1; ++j) {
- double v1 = matrix[vect[i - 1]][vect[i]] + matrix[vect[j]][vect[j + 1]];
- double v2 = matrix[vect[i - 1]][vect[j]] + matrix[vect[i]][vect[j + 1]];
- if (v1 > v2 && g > 0) {
- transport(i, j);
- i = 1;
- j = 2;
- --g;
- }
- }
- }
- }
- for (int i = 0; i < vect.size(); ++i) {
- std::cout << vect[i] + 1 << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement