【HDOJ】3007 Buried memory

來源:互聯網
上載者:User

標籤:

1. 題目描述
有n個點,求能覆蓋這n個點的半徑最小的圓的圓心及半徑。

2. 基本思路
演算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066
定義Di表示相對於P[1]和P[i]組成的最小覆蓋圓,如果P[2..i-1]都在這個圓內,那麼當前的圓心和半徑即為最優解。
如果P[j]不在這個圓內,那麼P[j]一定在新的最小覆蓋圓的邊界上即P[1]、P[j]、P[i]組成的圓。
因為三點可以確定一個圓,因此只需要不斷的找到不滿足的P[j],進而更新最優解即可。
其實就是三層迴圈,不斷更新最優解。然而,這個演算法的期望複雜度是O(n)。這個比較難以理解。

3. 代碼

  1 /* 3007 */  2 #include <iostream>  3 #include <sstream>  4 #include <string>  5 #include <map>  6 #include <queue>  7 #include <set>  8 #include <stack>  9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25  26 #define sti                set<int> 27 #define stpii            set<pair<int, int> > 28 #define mpii            map<int,int> 29 #define vi                vector<int> 30 #define pii                pair<int,int> 31 #define vpii            vector<pair<int,int> > 32 #define rep(i, a, n)     for (int i=a;i<n;++i) 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i) 34 #define clr                clear 35 #define pb                 push_back 36 #define mp                 make_pair 37 #define fir                first 38 #define sec                second 39 #define all(x)             (x).begin(),(x).end() 40 #define SZ(x)             ((int)(x).size()) 41 #define lson            l, mid, rt<<1 42 #define rson            mid+1, r, rt<<1|1 43  44 typedef struct { 45     double x, y; 46 } Point; 47  48 const double eps = 1e-6; 49 const int maxn = 505; 50 Point P[maxn]; 51 Point o; 52 double r; 53 int n; 54  55 double Length(Point a, Point b) { 56     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 57 } 58  59 double Cross(Point a, Point b, Point c) { 60     return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x); 61 } 62  63 Point Intersect(Point a, Point b, Point c, Point d) { 64     Point ret = a; 65     double t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) /  66                ((a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x));  67     ret.x += (b.x - a.x) * t; 68     ret.y += (b.y - a.y) * t; 69     return ret; 70 } 71  72 Point circumcenter(Point a, Point b, Point c) { 73     Point ua, ub, va, vb; 74      75     ua.x = (a.x + b.x) / 2.0; 76     ua.y = (a.y + b.y) / 2.0; 77     ub.x = ua.x - a.y + b.y; 78     ub.y = ua.y + a.x - b.x; 79      80     va.x = (a.x + c.x) / 2.0; 81     va.y = (a.y + c.y) / 2.0; 82     vb.x = va.x - a.y + c.y; 83     vb.y = va.y + a.x - c.x; 84     return Intersect(ua, ub, va, vb); 85 } 86  87 void min_center() { 88     o = P[0]; 89     r = 0; 90      91     rep(i, 1, n) { 92         if (Length(P[i], o)-r > eps) { 93             o = P[i]; 94             r = 0; 95             rep(j, 0, i) { 96                 if (Length(P[j], o)-r > eps) { 97                     o.x = (P[i].x + P[j].x) / 2; 98                     o.y = (P[i].y + P[j].y) / 2; 99                     r = Length(o, P[j]);100                     101                     rep(k, 0, j) {102                         if (Length(P[k], o)-r > eps) {103                             o = circumcenter(P[i], P[j], P[k]);104                             r = Length(o, P[k]);105                         }106                     }107                 }108             }109         }110     }111 }112 113 void solve() {114     min_center();115     printf("%.2lf %.2lf %.2lf\n", o.x, o.y, r);116 }117 118 int main() {119     ios::sync_with_stdio(false);120     #ifndef ONLINE_JUDGE121         freopen("data.in", "r", stdin);122         freopen("data.out", "w", stdout);123     #endif124     125     while (scanf("%d", &n)!=EOF && n) {126         rep(i, 0, n)127             scanf("%lf%lf", &P[i].x, &P[i].y);128         solve();129     }130     131     #ifndef ONLINE_JUDGE132         printf("time = %d.\n", (int)clock());133     #endif134     135     return 0;136 }

 

【HDOJ】3007 Buried memory

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.