模拟退火学习

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
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;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/132055859
今日推荐