模拟退火学习

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include 
#include 
#include 
#include 
#include 
using namespace std;

#define _CRT_SECURE_NO_WARNINGS

//void rfIO()
//{
//	FILE *stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

#define N 2000

struct node
{
	double x, y, w;
}e[N];
int n;
double ansx, ansy;
const double eps = 1e-15;
double f(double x, double y)
{
	double tot = 0;
	for (int i = 1; i <= n; i++)
	{
		double delx = x - e[i].x;
		double dely = y - e[i].y;
		tot += sqrt(delx*delx + dely*dely)*e[i].w;
	}
	return tot;
}
void mnth()
{
	double T = 100000;
	while (T>eps)
	{
		double nowx = ansx + (rand() * 2 - RAND_MAX)*T;
		double nowy = ansy + (rand() * 2 - RAND_MAX)*T;
		double delta = f(nowx, nowy) - f(ansx, ansy);
		if (delta<0)ansx = nowx, ansy = nowy;
		else if (exp(-delta / T)*RAND_MAX>rand())ansx = nowx, ansy = nowy;
		T *= 0.998;
	}
}
void slove(){
    for(int i = 1;i <= 2;i++)mnth();
}
int main()
{
	srand((int)time(NULL));
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> e[i].x >> e[i].y >> e[i].w;
		ansx += e[i].x; ansy += e[i].y;
	}
	ansx /= (double)n; ansy /= (double)n;
	slove();
	printf("%.3lf %.3lf\n", ansx, ansy);
	return 0;
}

你可能感兴趣的:(杂项,学习)