zoukankan      html  css  js  c++  java
  • 爬山法

    例题##

    同样是“吊打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
    ",ansx,ansy);
    	return 0;
    }
    
    
  • 相关阅读:
    洛谷 题解 P5595 【【XR-4】歌唱比赛】
    洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】
    洛谷 题解 CF299A 【Ksusha and Array】
    仙人掌找环
    2-SAT
    带花树
    帮我背单词
    csp2019退役祭
    P5284 [十二省联考2019]字符串问题 题解
    【网络流24题】魔术球问题
  • 原文地址:https://www.cnblogs.com/Mychael/p/8407177.html
Copyright © 2011-2022 走看看