[ 模拟退火 ] bzoj3860 平衡点

过了第一道模拟退火!!!


 

待填坑,,,

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5;
const double tt=1e-15,d=0.98;
int n;
double ansx,ansy,t;
struct node {int x,y,w;}a[N];

inline int read() {
	int x=0,f=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-')f=-1; c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}

double f(double x,double y) {
	double tot=0.0;
	for(int i=1;i<=n;i++) {
		double dx=x-a[i].x;
		double dy=y-a[i].y;
		tot+=sqrt(dx*dx+dy*dy)*a[i].w;
	}
	return tot;
}

void SA() {
    double T=150;
	while(T>tt) {
		double newx=ansx+(2*rand()-RAND_MAX)*T;
		double newy=ansy+(2*rand()-RAND_MAX)*T;
		double delta=f(ansx,ansy)-f(newx,newy);
		if(delta>0||exp(delta/t)*RAND_MAX>rand()) ansx=newx,ansy=newy;
		T*=d;
	}
}

int main() {
	n=read();
	for(int i=1;i<=n;i++) {
		a[i].x=read(),a[i].y=read(),a[i].w=read();
		ansx+=a[i].x,ansy+=a[i].y;
	}
	ansx=(double)ansx/n,ansy=(double)ansy/n;
    SA();
    printf("%.3lf %.3lf",ansx,ansy);
}

  

posted @ 2019-07-15 12:52  YeLingqi  阅读(147)  评论(0编辑  收藏  举报