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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[POJ2420]A Star not a Tree?(模拟退火)

發布時間:2024/10/12 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POJ2420]A Star not a Tree?(模拟退火) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://poj.org/problem?id=2420

求費馬點,即到所有其他點總和距離最小的點。

一開始想枚舉一個坐標,另一個坐標二分的,但是check的時候還是O(n)的,復雜度相當于O(n^2lgn),沒意義。

學習一種神貪心,模擬退火。感覺和啟發式搜索有點像啊,又有點像牛頓迭代。

  思路就是,固定一個點和一個步長,從這個點開始向四個方向擴展,擴展的步長就是當前步長。如果擴展到的點可以更新答案,那么記住這個點,也就是說這個貪心方向(梯度?)是正確的。就可以拿著這個點繼續沿著這個方向走了(剩余的方向可以不考慮了)。

  如果四個方向都不能走,說明步長過長,這個時候模擬退火,將步長按比率縮小。

1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <cassert> 8 #include <cstdio> 9 #include <bitset> 10 #include <vector> 11 #include <deque> 12 #include <queue> 13 #include <stack> 14 #include <ctime> 15 #include <set> 16 #include <map> 17 #include <cmath> 18 using namespace std; 19 20 typedef pair<double, double> pdd; 21 #define x first 22 #define y second 23 const double eps = 1e-8; 24 const double delta = 0.98; 25 const int maxn = 10100; 26 const int dx[5] = {1, -1, 0, 0}; 27 const int dy[5] = {0, 0, 1, -1}; 28 int n; 29 double ret; 30 pdd cur; 31 pdd p[maxn]; 32 33 double dist(pdd a, pdd b) { 34 double p = a.x - b.x; 35 double q = a.y - b.y; 36 return sqrt(p * p + q * q); 37 } 38 39 int main() { 40 // freopen("in", "r", stdin); 41 while(~scanf("%d", &n)) { 42 for(int i = 0; i < n; i++) { 43 scanf("%lf %lf", &p[i].x, &p[i].y); 44 } 45 int t = 100; 46 ret = 1e100; 47 cur = pdd(.0, .0); 48 while(t > eps) { 49 bool flag = 1; 50 while(flag) { 51 flag = 0; 52 for(int i = 0; i < 4; i++) { 53 pdd pos = pdd(cur.x+dx[i]*t, cur.y+dy[i]*t); 54 double tmp = .0; 55 for(int j = 0; j < n; j++) { 56 tmp += dist(pos, p[j]); 57 } 58 if(ret - tmp > eps) { 59 ret = tmp; 60 cur = pos; 61 flag = 1; 62 break; 63 } 64 } 65 } 66 t *= delta; 67 } 68 printf("%.0f\n", ret); 69 } 70 return 0; 71 }

?

轉載于:https://www.cnblogs.com/kirai/p/6229822.html

總結

以上是生活随笔為你收集整理的[POJ2420]A Star not a Tree?(模拟退火)的全部內容,希望文章能夠幫你解決所遇到的問題。

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