Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <memory.h>
- #include <cmath>
- #include <time.h>
- #include <climits>
- #include <boost/random.hpp>
- #define PI 3.1415926535897932384626433832795
- #define sqr(x) ((x)*(x))
- #define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl
- #define sqr(x) ((x)*(x))
- typedef long long LL;
- typedef unsigned long long ULL;
- using namespace std;
- const int ant_count = 10;
- const int max_n = 105;
- const long double minus_coef = 1e-7;
- const long double plus_coef = 5;
- const long double alpha = 2;
- const long double beta = 5;
- const long double up_edge = 1e100;
- int n;
- long double W[max_n][max_n], D[max_n][max_n], T[max_n];
- struct Tant {
- int pos;
- int mem[max_n], memn, p[max_n];
- long double curlen;
- } ant[ant_count];
- struct Tp {
- long double x, y;
- } a[max_n];
- int main(){
- freopen("input.txt","r",stdin);
- boost::random::mt19937 gen(time(0));
- srand((unsigned)time(NULL));
- cin >> n;
- for(int i=0;i<n;i++) {
- cin >> a[i].x >> a[i].y;
- for(int j=0;j<i;j++) {
- D[i][j] = D[j][i] = sqrt(sqr(a[i].x - a[j].x) + sqr(a[i].y - a[j].y));
- W[i][j] = W[j][i] = 1;
- }
- }
- long double ans = 1e100;
- int iter = 0;
- while (true) {
- iter++;
- for(int i=0; i<ant_count && i < iter; ++i) {
- int x = ant[i].pos;
- if(ant[i].memn == n - 1) {
- long double l = ant[i].curlen + D[x][0];
- ant[i].p[0] = ant[i].pos;
- if(l < ans){
- ans = l;
- ofstream f("res.txt");
- f << ans << endl;
- int y = 0, len = 0;
- do {
- f << (y + 1) << " ";
- y = ant[i].p[y];
- } while(y != 0);
- f << endl;
- f.close();
- cout << iter << " : " << ans << endl;
- }
- int y = 0;
- do {
- W[ant[i].p[y]][y] = min(W[ant[i].p[y]][y] + plus_coef / l, up_edge);
- y = ant[i].p[y];
- } while(y != 0);
- ant[i].pos = 0;
- ant[i].curlen = 0;
- ant[i].memn = 0;
- memset(ant[i].mem, 0, sizeof(ant[i].mem));
- continue;
- }
- double sum = 0;
- for(int j = 1; j < n; ++j) if (!ant[i].mem[j]) {
- T[j] = pow(W[x][j], alpha) + pow(D[x][j], -beta);
- sum += T[j];
- }
- double r = boost::random::uniform_real_distribution<double>(0, sum)(gen);
- for(int j = 1; j < n; ++j) if (!ant[i].mem[j]) {
- if (r < T[j]) {
- ant[i].pos = j;
- ant[i].mem[j] = true;
- ant[i].memn++;
- ant[i].curlen += D[x][j];
- ant[i].p[j] = x;
- break;
- }
- r -= T[j];
- }
- }
- long double max = -1e100, min = 1e100;
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++) if(i != j) {
- W[i][j] = (1 - minus_coef) * W[i][j];
- // if(W[i][j] > max) max = W[i][j];
- // if(W[j][i] < min) min = W[j][i];
- }
- // cerr << min << " " << max << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment