Description
求 n n 个点的带权类费马点。
Solution
显然此题可以用爬山算法或者模拟退火求解。
具体算法请见我的博客:「算法笔记」模拟退火
时间复杂度:
Θ(rp)
Θ
(
rp
)
(都是乱搞算法了还求什么复杂度啊)
Code
爬山算法:
#include <cstdio>
#include <cmath>
const int N=10005;
int n,x[N],y[N],w[N];
double ansx,ansy;
void hillclimb() {
double t=1000;
while(t>1e-8) {
double nowx=0,nowy=0;
for(int i=1;i<=n;++i) {
double dx=x[i]-ansx,dy=y[i]-ansy;
double dis=sqrt(dx*dx+dy*dy);
nowx+=(x[i]-ansx)*w[i]/dis;
nowy+=(y[i]-ansy)*w[i]/dis;
}
ansx+=nowx*t,ansy+=nowy*t;
if(t>0.5) t*=0.5; else t*=0.97;
}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i) {
scanf("%d%d%d",&x[i],&y[i],&w[i]);
ansx+=x[i],ansy+=y[i];
}
ansx/=n,ansy/=n;
hillclimb();
printf("%.3lf %.3lf\n",ansx,ansy);
return 0;
}
模拟退火:
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
const int N=10005;
int n,x[N],y[N],w[N];
double ansx,ansy,dis;
double Rand() {
return (double)rand()/RAND_MAX;
}
double calc(double xx,double yy) {
double res=0;
for(int i=1;i<=n;++i) {
double dx=x[i]-xx,dy=y[i]-yy;
res+=sqrt(dx*dx+dy*dy)*w[i];
}
if(res<dis) dis=res,ansx=xx,ansy=yy;
return res;
}
void simulateAnneal() {
double t=100000;
double nowx=ansx,nowy=ansy;
while(t>0.001) {
double nxtx=nowx+t*(Rand()*2-1);
double nxty=nowy+t*(Rand()*2-1);
double delta=calc(nxtx,nxty)-calc(nowx,nowy);
if(exp(-delta/t)>Rand()) nowx=nxtx,nowy=nxty;
t*=0.97;
}
for(int i=1;i<=1000;++i) {
double nxtx=ansx+t*(Rand()*2-1);
double nxty=ansy+t*(Rand()*2-1);
calc(nxtx,nxty);
}
}
int main() {
srand(time(0));
scanf("%d",&n);
for(int i=1;i<=n;++i) {
scanf("%d%d%d",&x[i],&y[i],&w[i]);
ansx+=x[i],ansy+=y[i];
}
ansx/=n,ansy/=n,dis=calc(ansx,ansy);
simulateAnneal();
printf("%.3lf %.3lf\n",ansx,ansy);
return 0;
}