Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fin cin
- #define fout cout
- //ifstream fin("x.in"); ofstream fout("x.out");
- const int nmax = 500;
- int n;
- typedef pair<double, double> pct;
- int indice[nmax + 1];
- pct v[nmax + 1];
- bool fol[nmax + 1];
- inline double dist (pct a, pct b) {
- return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
- }
- pair<double, vector<int>> solve () {
- int s = rand() % n + 1;
- //fout << s <<"\n";
- for (int i = 1; i <= n; ++ i)
- fol[i] = 0;
- int x = s;
- double sum = 0;
- vector<int> ord;
- ord.push_back(s);
- for (int i = 2; i <= n; ++ i) {
- fol[x] = 1;
- int ind = -1;
- double mndist = INFINITY;
- for (int j = 1; j <= n; j ++) {
- if (fol[j]) continue;
- double aux = dist(v[x], v[j]);
- if (aux < mndist) {
- mndist = aux;
- ind = j;
- }
- }
- sum += mndist;
- x = ind;
- ord.push_back(x);
- }
- sum += dist(v[s], v[x]);
- ord.push_back(s);
- return {sum, ord};
- }
- int main () {
- fin >> n;
- for (int i = 1; i <= n; ++ i) {
- int x;
- fin >> x; indice[i] = x;
- fin >> v[i].first >> v[i].second;
- }
- double cst = clock();
- double best = INFINITY;
- vector<int> ans;
- srand(time(0));
- for (int pas = 0; ; ++ pas) {
- if (pas % 10 == 0 && (clock() - cst) / CLOCKS_PER_SEC > 4.8) {
- break;
- }
- pair<double, vector<int>> ans2 = solve();
- if (ans2.first < best) {
- best = ans2.first;
- ans = ans2.second;
- }
- }
- //fout << best << "\n";
- for (auto i : ans)
- fout << indice[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement