[hdu2899]Strange fuction

题目链接

[hdu2899Strange fuction]

题解

发现这个东西是个凸的,直接三分
但我想写模拟退火...

代码

#include<cmath> 
#include<cstdio> 
#include<algorithm> 
inline int read() { 
    int x = 0,f = 1; 
    char c = getchar(); 
    while(c < '0' || c > '9')c = getchar(); 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
#define INF 1e18 
#define eps 1e-10 
int n; 
struct Node {
    double x,y; 
} p[101]; 
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 calc(Node a) { 
    double sum = 0; 
    for(int i = 1;i <= n;++ i)  
        sum += dis(a,p[i]); 
    return sum; 
} 
#define T 100 
#define delta 0.98 

double Rand() { 
    return (double) rand() / (double)RAND_MAX;  
} 
#define IT 5 
double dx[7] = {1,0,-1,0,1}; 
double solve() { 
    Node x = p[1]; 
    double res = calc(x); 
    double t = T; 
    while(t > eps) { 
        for(int i = 1;i < IT; ++ i) {
            for(int i = 0;i <= 4;++ i) { 
                Node nx = x; 
                nx.x += dx[i] * Rand() * t; 
                nx.y += dx[i + 1] * Rand() * t; 
                double nxres = calc(nx); 
                if(nxres < res || Rand() >= exp((nxres - res) / res)) res = nxres,x = nx; 
            } 
        } 
        t *= delta; 
    } 
    return res; 
} 
int main() { 
    srand(20010901); 
    n = read(); 
    for(int i = 1;i <= n;++ i) 
        scanf("%lf%lf",&p[i].x,&p[i].y); 
    double ans = INF; 
    for(int i = 1;i <= 10;++ i) { 
        ans = std:: min(ans,solve()); 
    } 
    printf("%.0lf\n",ans); 
} 

转载于:https://www.cnblogs.com/sssy/p/9640786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值