题解:
根据模拟退火暴力转移点即可。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
inline int rd() {
char ch=getchar(); int i=0,f=1;
while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=getchar();}
return i*f;
}
struct P {
double x,y;
P(double x=0,double y=0) :x(x),y(y){}
friend inline P operator +(const P &a,const P &b) {
return P(a.x+b.x,a.y+b.y);
}
friend inline P operator -(const P &a,const P &b) {
return P(a.x-b.x,a.y-b.y);
}
inline double dis() {
return sqrt(x*x+y*y);
}
}p[110],now;
int n;
const double PI=acos(-1.0);
inline double rnd() {
return (rand()%10000)/10000.0;
}
inline double calc(const P &t) {
double rs=0;
for(int i=1;i<=n;i++) rs=rs+(t-p[i]).dis();
return rs;
}
int main() {
n=rd();
for(int i=1;i<=n;i++) p[i].x=rd(),p[i].y=rd();
now=p[1]; double ans=calc(now),lst=ans;
for(double st=10000; st>=0.0000001; st=st*0.998) {
double theta=rand()%360;
theta=theta/180.0*PI;
double detx=st*cos(theta);
double dety=st*sin(theta);
P tp=now+P(detx,dety);
double tpans=calc(tp);
ans=min(ans,tpans);
if(tpans<lst || exp((lst-tpans)/st)>rnd()) now=tp,lst=tpans;
}
printf("%.0f\n",ans);
}