zoukankan      html  css  js  c++  java
  • POJ2069:Super Star

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html

    我对爬山的理解:https://www.cnblogs.com/AKMer/p/9555215.html

    题目传送门:http://poj.org/problem?id=2069

    这题和求费马点没啥多大区别……就是把二维改成三维了,然后原本求距离和改成求最远距离了。

    如果你不知道费马点是啥的话可以去看看这篇博客:

    POJ2420 A Star not a Tree?:https://www.cnblogs.com/AKMer/p/9594350.html

    时间复杂度:(O(能A))

    空间复杂度:(O(能A))

    爬山算法代码如下:

    #include <cmath>
    #include <ctime>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    #define sqr(a) ((a)*(a))
    
    int n;
    double ansx,ansy,ansz,ans;
    
    struct point {
    	double x,y,z;
    }p[35];
    
    double len() {
    	double x=rand()%200000-100000;
    	return x/100000;
    }
    
    double dis(double x1,double y1,double z1,double x2,double y2,double z2) {
    	return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));
    }
    
    double calc(double x,double y,double z) {
    	double tmp=0;
    	for(int i=1;i<=n;i++)
    		tmp=max(tmp,dis(x,y,z,p[i].x,p[i].y,p[i].z));//改成取max
    	if(tmp<ans)ans=tmp;
    	return tmp;
    }
    
    int main() {
    	srand(time(0));
    	while(~scanf("%d",&n)) {
    		if(!n)break;ans=1e18;
            ansx=0,ansy=0,ansz=0;
    		for(int i=1;i<=n;i++) {
    			scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
    			ansx+=p[i].x,ansy+=p[i].y,ansz+=p[i].z;
    		}ansx/=n,ansy/=n,ansz/=n;
    		double now_x=ansx,now_y=ansy,now_z=ansz;
    		for(double T=1e7;T>=1e-7;T*=0.998) {
    			double nxt_x=now_x+len()*T;
    			double nxt_y=now_y+len()*T;
    			double nxt_z=now_z+len()*T;
    			if(calc(nxt_x,nxt_y,nxt_z)<calc(now_x,now_y,now_z))
    				now_x=nxt_x,now_y=nxt_y,now_z=nxt_z;
    		}printf("%.5lf
    ",ans);
    	}
    	return 0;
    }
    

    模拟退火代码如下:

    #include <cmath>
    #include <ctime>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    #define sqr(a) ((a)*(a))
    
    const double T_0=1e-7;
    const double del_T=0.998;
    
    int n;
    double ansx,ansy,ansz,ans;
    
    struct point {
    	double x,y,z;
    }p[35];
    
    double len() {
    	double x=rand()%200000-100000;
    	return x/100000;
    }
    
    double dis(double x1,double y1,double z1,double x2,double y2,double z2) {
    	return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));
    }
    
    double calc(double x,double y,double z) {
    	double tmp=0;
    	for(int i=1;i<=n;i++)
    		tmp=max(tmp,dis(x,y,z,p[i].x,p[i].y,p[i].z));
    	if(tmp<ans)ans=tmp;
    	return tmp;
    }
    
    void Anneal() {
    	double T=1e7,now_x=ansx,now_y=ansy,now_z=ansz;
    	while(T>=T_0) {
    		double nxt_x=now_x+len()*T;
    		double nxt_y=now_y+len()*T;
    		double nxt_z=now_z+len()*T;
    		double tmp1=calc(now_x,now_y,now_z);
    		double tmp2=calc(nxt_x,nxt_y,nxt_z);
    		if(tmp1>tmp2||exp((tmp1-tmp2)/T)*RAND_MAX>rand())
    			now_x=nxt_x,now_y=nxt_y,now_z=nxt_z;
    		T*=del_T;
    	}
    }
    
    int main() {
    	while(~scanf("%d",&n)) {
    		if(!n)break;ans=1e18;
    		ansx=0,ansy=0,ansz=0;
    		for(int i=1;i<=n;i++) {
    			scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
    			ansx+=p[i].x,ansy+=p[i].y,ansz+=p[i].z;
    		}ansx/=n,ansy/=n,ansz/=n;
    		Anneal();printf("%.5lf
    ",ans);
    	}
    	return 0;
    }
    

    POJ数据好强啊!!!根本A不掉啊!

  • 相关阅读:
    《DSP using MATLAB》 示例 Example 9.12
    《DSP using MATLAB》示例 Example 9.11
    《DSP using MATLAB》示例 Example 9.10
    《DSP using MATLAB》示例Example 9.9
    《DSP using MATLAB》示例 Example 9.8
    《DSP using MATLAB》示例Example 9.7
    《DSP using MATLAB》示例 Example 9.6
    《DSP using MATLAB》示例Example 9.5
    《DSP using MATLAB》示例 Example 9.4
    (转载)【C++11新特性】 nullptr关键字
  • 原文地址:https://www.cnblogs.com/AKMer/p/9597381.html
Copyright © 2011-2022 走看看