星星还是树 AcWing-3167 模拟退火

星星还是树

在这里插入图片描述

code

/*Siberian Squirrel*/
/*Cute JinFish*/
#include<bits/stdc++.h>

#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ACM_LOCAL

using namespace std;
typedef long long ll;

const double PI = acos(-1);
const double eps = 1e-8;
/*const int MOD = 998244353, r = 119, k = 23, g = 3;
const int MOD = 1004535809, r = 479, k = 21, g = 3;*/
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int M = 1e7 + 10;
const int N = 4e5 + 10;

int fx[5] = {
    
    0, 0, 1, -1};
int fy[5] = {
    
    1, -1, 0, 0};

int dcmp(double x) {
    
    
    if(fabs(x) < eps) return 0;
    else return x < 0? -1: 1;
}
//inline int rnd(){static int seed=2333;return seed=(((seed*666666ll+20050818)%998244353)^1000000007)%1004535809;}
//double Rand() {return (double)rand() / RAND_MAX;}

struct node {
    
    
    double x, y;
}p[N], _p;
int n;

double dis(node a, node b) {
    
    
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double func(node a) {
    
    
    double res = 0;
    for(int i = 1; i <= n; ++ i) {
    
    
        res += dis(p[i], a);
    }
    return res;
}

inline void solve(double res = 0, bool flag = false) {
    
    
    double x = 0, y = 0;
    double t = 10000, xx, yy;
    _p.x = x, _p.y = y;
    while(t > eps) {
    
    
        res = func(_p);
        flag = false;
        for(int i = 0; i < 4; ++ i) {
    
    
            xx = x + t * fx[i];
            yy = y + t * fy[i];
            if(xx > 10000 || yy > 10000 || xx < 0 || yy < 0) continue;
            _p.x = xx, _p.y = yy;
            if(func(_p) < res) {
    
    
                flag = true;
                break;
            }
        }
        if(flag) x = xx, y = yy;
        else t *= 0.618;
        _p.x = x, _p.y = y;
    }
    res = func(_p);
    cout << (int)res + (res - (int)res > 0.5000? 1: 0) << endl;
}


int main() {
    
    
    IO;
// srand(time(0));
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
    int o = 1;
//	cin >> o;
    while(o --) {
    
    
        cin >> n;
        for(int i = 1; i <= n; ++ i) {
    
    
            cin >> p[i].x >> p[i].y;
        }
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46173805/article/details/114532555