• [ 模拟退火 ] 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);
    }
    

      

  • 相关阅读:
    POJ 1306.Combinations
    HDU 5640.King's Cake
    HDU 1072.Nightmare
    HDU 2717.Catch That Cow
    HDU 1372.Knight Moves
    HDU 1548.A strange lift
    AOJ 802.运输宝物
    AOJ 794.西瓜理发记(二)
    AOJ 793.西瓜理发记(一)
    AOJ 789.买酒
  • 原文地址:https://www.cnblogs.com/qq8260573/p/11188229.html
Copyright © 2020-2023  润新知