日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 2069 Super Star 最小求覆盖【爬山算法】

發布時間:2025/4/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 2069 Super Star 最小求覆盖【爬山算法】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:給定幾個點,要求覆蓋這些點的最小球半徑。(求到n個點的最大距離最小化的點
因為是單峰的所以可以用爬山算法
主要是我的退火算法板子精度達不到??

?

//#include<bits/stdc++.h> #include <iostream> #include <cmath> #include <cstdio> #include <stdlib.h> #include <ctime> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 1e4 + 5; int n; double X,Y; struct point {double x,y,z; } p[maxn],pp; double ans=1e10; double dis(point a,point b) {return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); } int get(point x) {double res=-1;int k;for(int i=1; i<=n; ++i){double m=dis(p[i],x);if(m>res)res=m,k=i;}ans=min(ans,dis(x,p[k]));return k; } void hc() {double T=1,eps=1e-10;while(T>eps){//if(pp.x<=X&&pp.y<=Y&&pp.x>=0&&pp.y>=0)// {int k=get(pp);pp.x=pp.x+(p[k].x-pp.x)*T;pp.y=pp.y+(p[k].y-pp.y)*T;pp.z=pp.z+(p[k].z-pp.z)*T;// }T*=0.9996;} } int main() {double X,Y;while(scanf("%d",&n)!=EOF){if(n==0)break;pp.x=pp.y=pp.z=0;for(int i=1; i<=n; ++i){cin>>p[i].x>>p[i].y>>p[i].z;pp.x+=p[i].x,pp.y+=p[i].y,pp.z+=p[i].z;}pp.x/=n;pp.y/=n;pp.z/=n;ans=1e10;hc();// printf("(%.1f,%.1f).\n",pp.x,pp.y,pp.z);printf("%.5f\n",sqrt(ans));}return 0; }

退火沒有A掉:
?

///退火算法 #include <iostream> #include <cmath> #include <algorithm> #include <cstdio> #include <stdlib.h> #include <ctime> #define mp make_pair #define io ios::sync_with_stdio(0),cin.tie(0) using namespace std; typedef long long ll; typedef pair<int, int> PII; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 1e6 + 5; int n, x, y; struct point { double x, y, z; } p[maxn], now, nex, ansp;double dis(point a, point b) { return ((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z)); }double f(point x) { //評估函數double res = 0;for (int i = 1;i <= n;i++) res = max(res, dis(x, p[i]));return res; } long double ans = 1e20;//最開始的能量值,初始很大就可以,不用修改 void sa() {ans = 1e22;double T = 100; //初始溫度, (可以適當修改,最好和給的數據最大范圍相同,或者縮小其原來0.1)double d = 0.9999; //降溫系數 (可以適當修改,影響結果的精度和循環的次數,)double eps = 1e-10; //最終溫度 (要是因為精度問題,可以適當減小最終溫度)double TT = 1.0; //采納新解的初始概率double dd = 0.999; //(可以適當修改,采納新解變更的概率)(這個概率下面新解更新的時候,最好和未采納的新解更新的次數是一半一半)double res = f(now); //傳入的初始默認解(now)下得到的評估能量值if (res < ans) ans = res, ansp = now;//ansp終解int cnt=0;while (T > eps){for (int i = -1;i <= 1;++i)for (int j = -1;j <= 1;++j)if ((now.x+T*i<=100)&&(now.x+T*i>=0)&&(now.y+T*j<=100)&&(now.y+T*j>=0)){nex.x = now.x + T * i, nex.y = now.y + T * j;//新解double tmp = f(nex);//新解下的評估能量值if (tmp < ans) ans = tmp, ansp = nex;//降溫成功,更新當前最優解if (tmp < res) res = tmp, now = nex;// 降溫成功,采納新解else if (TT > rand() % 10000 / 10000.0) res = tmp, now = nex;//,cout<<"======"<<endl;//沒有 降溫成功,但是以一定的概率采納新解//else cout<<"="<<endl;//用于測試,設定的采納新解的概率,是否為一半一半,可以適當修改降溫參數dd}cnt++;T *= d; TT *= dd;}//cout<<cnt<<endl; } int main() {srand(time(0));while (scanf("%d", &n)!=EOF) {if (n == 0)break;now.x = now.y = 0, now.z = 0;for (int i = 1;i <= n;++i){cin >> p[i].x >> p[i].y >> p[i].z;//scanf("%lf%lf%lf",&p[i].x,&p[i].y),now.x += p[i].x, now.y += p[i].y, now.z += p[i].z;}now.x = now.y = 55, now.z = 55;sa();//now.x /= n, now.y /= n, now.z /= n;sa();//now.x = now.y = 20, now.z = 20;sa();printf("%.5Lf\n", sqrt(ans));//cout<<ans<<endl;}return 0; }

?

總結

以上是生活随笔為你收集整理的poj 2069 Super Star 最小求覆盖【爬山算法】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。