hdu 携程全球数据中心建设 (球面距离 + 最小生成树)

题目

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <algorithm>
  7 using namespace std;
  8 double G[110][110], sum;
  9 const double pi = acos(-1);
 10 const int INF = (1<<28);
 11 
 12 struct node
 13 {
 14     double x, y;    //x是经度, y是纬度。
 15 }p[110];
 16 
 17 double angle(double lng1, double lat1, double lng2, double lat2)
 18 {
 19     double dlng = fabs(lng1-lng2)*pi/180;
 20     while(dlng >= pi+pi)
 21         dlng -= (pi+pi);
 22     if(dlng > pi)
 23         dlng = pi+pi-dlng;
 24     lat1 *= pi/180;
 25     lat2 *= pi/180;
 26     return acos(cos(lat1)*cos(lat2)*cos(dlng)+sin(lat1)*sin(lat2));
 27 }
 28 
 29 double sphere_dist(double r, double lng1, double lat1, double lng2, double lat2)
 30 {
 31     return r*angle(lng1, lat1, lng2, lat2);
 32 }
 33 
 34 void prime(int n)
 35 {
 36     int i, j, pos;
 37     int Min, vis[110];
 38     double d[110];
 39     memset(vis, 0, sizeof(vis));
 40     memset(d, 0, sizeof(d));
 41 
 42     for(i = 1; i <= n; i++)
 43     d[i] = G[1][i];
 44     vis[1] = 1;
 45     for(i = 2; i <= n; i++)
 46     {
 47         Min = INF;
 48         for(j = 1; j <= n; j++)
 49         {
 50             if(!vis[j] && Min>d[j])
 51             {
 52                 Min = d[j];
 53                 pos = j;
 54             }
 55         }
 56         sum += Min;
 57         vis[pos] = 1;
 58         for(j = 1; j <= n; j++)
 59         {
 60             if(!vis[j])
 61             {
 62                 if(d[j] > G[pos][j])
 63                     d[j] = G[pos][j];
 64             }
 65         }
 66     }
 67 }
 68 int main()
 69 {
 70     int n, t, c, i, j;
 71     double d, l, r;
 72     cin>>t;
 73     while(t--)
 74     {
 75         sum = 0;
 76         cin>>d>>l>>c;
 77         r = d/2*1.0;
 78         n = c;
 79         for(i = 1; i <= n; i++)
 80         {
 81             for(j = 1; j <= n; j++)
 82                 G[i][j] = INF;
 83             G[i][i] = 0;
 84         }
 85         for(i = 1; i <= n; i++)
 86             cin>>p[i].x>>p[i].y;
 87 
 88         for(i = 1; i <= n; i++)
 89             for(j = i+1; j <= n; j++)
 90         {
 91             G[i][j] = sphere_dist(r, p[i].y, p[i].x, p[j].y, p[j].x);
 92             G[j][i] = G[i][j];
 93         }
 94         prime(n);
 95         if(sum - l> 0)
 96             cout<<"N"<<endl;
 97         else
 98         cout<<"Y"<<endl;
 99     }
100     return 0;
101 }

 

posted @ 2014-04-11 21:20  水门  阅读(232)  评论(0编辑  收藏  举报