爬山法

例题##

同样是“吊打XXX”
同JSOI平衡点

爬山法##

其实很简单,就是每次往最优的方向移动一段距离,随着距离的接近而放小移动幅度,最后逼近最优解

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 10005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
	return out * flag;
}
int n,w[maxn];
double x[maxn],y[maxn];
double ansx,ansy;
double dis(double X,double Y,int i){
	return sqrt((X - x[i]) * (X - x[i]) + (Y - y[i]) * (Y - y[i]));
}
void climbhill(){
	double T = 10000,dx,dy;
	while (T > 0.00000001){
		dx = dy = 0;
		for (int i = 1; i <= n; i++){
			dx += (x[i] - ansx) * w[i] / dis(ansx,ansy,i);
			dy += (y[i] - ansy) * w[i] / dis(ansx,ansy,i);
		}
		ansx += dx * T;
		ansy += dy * T;
		if (T > 0.5) T *= 0.5;
		else T *= 0.97;
	}
}
int main(){
	n = read();
	for (int i = 1; i <= n; i++){
		x[i] = read(),y[i] = read(),w[i] = read();
		ansx += x[i] * w[i]; ansy += y[i] * w[i];
	}
	ansx /= n; ansy /= n;
	climbhill();
	printf("%.3lf %.3lf\n",ansx,ansy);
	return 0;
}

原文链接: https://www.cnblogs.com/Mychael/p/8407177.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    爬山法

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/268784

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午7:39
下一篇 2023年2月14日 下午7:42

相关推荐