码迷,mamicode.com
首页 > 其他好文 > 详细

P1265 公路修建 (最小生成树)

时间:2020-02-04 11:02:51      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:else   clu   str   eof   style   can   tps   +=   fine   

题目链接

技术图片

 

 

 1 #include <bits/stdc++.h>
 2 # define LL long long
 3 using namespace std;
 4 
 5 const int maxn=5000+10;
 6 int n;
 7 LL dis[maxn];
 8 int complete[maxn];
 9 
10 struct City{
11     LL x,y;
12 }cities[maxn];
13 
14 LL operator*(City &c1, City &c2){
15     return (c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y);
16 }
17 
18 int main(){
19     scanf("%d", &n);
20     for(int i=1;i<=n;++i){
21         scanf("%lld %lld", &cities[i].x, &cities[i].y);
22     }
23     memset(dis,127,sizeof(dis));
24     dis[1]=0;
25     for(int i=1;i<=n-1;++i){
26         int u=0;
27         for(int j=1;j<=n;++j){
28             if(complete[j]==1) continue;
29             if(u==0) u=j;
30             else{
31                 if(dis[j]<dis[u]) u=j;
32             }
33         }
34         complete[u]=1;
35         for(int j=1;j<=n;++j){
36             if(complete[j]==1) continue;
37             dis[j]=min(dis[j],cities[u]*cities[j]);
38         }
39     }
40     double res=0;
41     for(int i=1;i<=n;++i){
42         res+=sqrt((double)dis[i]);
43     }
44     printf("%.2f", res);
45     return 0;
46 }

 

P1265 公路修建 (最小生成树)

标签:else   clu   str   eof   style   can   tps   +=   fine   

原文地址:https://www.cnblogs.com/FEIIEF/p/12258346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!