洛谷P1265 公路修建

 1 /*
 2 用kuskal会被卡
 3 一边prim一边算两点间距离(边权)
 4 否则会MLE
 5 另:不开long long见祖宗 
 6 */
 7 #include<bits/stdc++.h>
 8 using namespace std;
 9 const int maxn=5005;
10 const int INF=0x3f3f3f3f;
11 typedef pair<double,int> P;
12 priority_queue< P,vector<P>,greater<P> >q;
13 bool v[maxn];
14 int n,m;
15 double ans,dis[maxn];
16 struct point{int x,y;}d[maxn];
17 double Dis(const point &x,const point &y){return sqrt((long long)(x.x-y.x)*(x.x-y.x)+(long long)(x.y-y.y)*(x.y-y.y));}
18 void prim()
19 {
20     fill(dis,dis+1+n,INF);dis[1]=0;
21     q.push(make_pair(0,1));
22     while(!q.empty())
23     {
24         int Top=q.top().second;q.pop();
25         if(v[Top]) continue;ans+=dis[Top];v[Top]=1;
26         for(int i=1;i<=n;++i)
27         {
28             if(!v[i]) 
29             {
30                 double tmp=Dis(d[i],d[Top]);
31                 if(tmp<dis[i])
32                 {
33                     dis[i]=tmp;
34                     q.push(make_pair(dis[i],i));
35                 }
36             }
37         }
38     }
39 }
40 int main()
41 {
42     scanf("%d",&n);
43     for(int i=1;i<=n;++i) scanf("%d%d",&d[i].x,&d[i].y);
44     prim();
45     printf("%.2lf",ans);
46     return 0;
47 }

 

posted @ 2019-01-13 10:21  宇興  阅读(224)  评论(0编辑  收藏  举报