2014携程初赛 1003 携程全球数据中心建设

http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=23003&pid=1003

题解:已知经纬度,求两点之间距离。然后最小生成树……

  1 //
  2 //  main.cpp
  3 //  xiecheng3
  4 //
  5 //  Created by zhang on 14-4-10.
  6 //  Copyright (c) 2014年 apple. All rights reserved.
  7 //
  8 
  9 //
 10 //  main.cpp
 11 //  POJ 1258
 12 //
 13 //  Created by zhang on 14-3-31.
 14 //  Copyright (c) 2014年 apple. All rights reserved.
 15 //
 16 
 17 #include <iostream>
 18 #include <cstdio>
 19 #include <algorithm>
 20 #include <cmath>
 21 
 22 using namespace std;
 23 
 24 const int maxn=1100;
 25 const int INF=1001000;
 26 int cost[maxn][maxn];
 27 int MINC[maxn];
 28 bool used[maxn];
 29 int V;
 30 long long loc[maxn];
 31 double lat[maxn],lng[maxn];
 32 const double PI=3.14159265358979323846;
 33 double r ;
 34 int DIS[maxn];
 35 
 36 double rad(double d)
 37 
 38 {
 39     
 40     return d * PI / 180.0;
 41     
 42 }
 43 
 44 
 45 
 46 double getDistance(double lat1, double lng1, double lat2, double lng2)
 47 
 48 {
 49     
 50     double radLat1 = rad(lat1);
 51     
 52     double radLat2 = rad(lat2);
 53     
 54     double radLng1 = rad(lng1);
 55     
 56     double radLng2 = rad(lng2);
 57     
 58     double s = acos(sin(radLat1)*sin(radLat2)+cos(radLat1)*cos(radLat2)*cos(radLng1-radLng2));
 59     
 60     s = s * r;
 61     
 62     return s;
 63     
 64 }
 65 
 66 int prim()
 67 {
 68     for (int i=0; i<V; i++) {
 69         MINC[i]=INF;
 70         used[i]=false;
 71     }
 72     MINC[0]=0;
 73     int res=0;
 74     while (1) {
 75         int v=-1;
 76         for (int u=0; u<V; u++) {
 77             if (!used[u]&&(v==-1||MINC[u]<MINC[v])) {
 78                 v=u;
 79             }
 80         }
 81         if (v==-1) {
 82             break;
 83         }
 84         used[v]=true;
 85         res+=MINC[v];
 86         for (int u=0; u<V; u++) {
 87             MINC[u]=min(MINC[u],cost[v][u]);
 88         }
 89     }
 90     return res;
 91 }
 92 
 93 
 94 int main()
 95 {
 96     //freopen("/Users/apple/Desktop/xiecheng3/xiecheng3/in", "r", stdin);
 97     //freopen("/Users/apple/Desktop/xiecheng3/xiecheng3/out", "w", stdout);
 98     int N;
 99     int L,C;
100     double D;
101     
102     
103     scanf("%d",&N);
104     for (int i=0; i<N; i++) {
105         D=0;
106         L=0;
107         C=0;
108         scanf("%lf",&D);
109     //printf("%lf
",D);
110         r=D/2;
111         scanf("%d",&L);
112     //printf("%d
",L);
113         scanf("%d",&C);
114         V=C;
115     //printf("%d
",C);
116         for (int i=0; i<C; i++) {
117             cin>>lat[i]>>lng[i];
118         }
119         for (int i=0; i<C; i++) {
120             for (int j=0; j<C; j++) {
121                 cost[i][j]=getDistance(lat[i],lng[i],lat[j],lng[j]);
122 //printf("cost %d %d %d
",cost[i][j],i,j);
123             }
124         }
125 //printf("%d
",prim());
126         if (prim()>L) {
127             printf("N
");
128         }
129         if (prim()<=L) {
130             printf("Y
");
131         }
132     }
133     return 0;
134 }
原文地址:https://www.cnblogs.com/der-z/p/3658074.html