[POJ2420]A Star not a Tree?(類比退火)

來源:互聯網
上載者:User

標籤:距離   style   啟發學習法   double   思路   bsp   nbsp   方向   using   

題目連結: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 first22 #define y second23 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 }

 

[POJ2420]A Star not a Tree?(類比退火)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.