前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ-----(1162)Eddy's picture(最小生成树)

HDUOJ-----(1162)Eddy's picture(最小生成树)

作者头像
Gxjun
发布2018-03-26 10:37:20
5060
发布2018-03-26 10:37:20
举报
文章被收录于专栏:ml

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6821    Accepted Submission(s): 3444

Problem Description

Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you. Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?

Input

The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.  Input contains multiple test cases. Process to the end of file.

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points. 

Sample Input

3 1.0 1.0 2.0 2.0 2.0 4.0

Sample Output

3.41

Author

eddy

此题数据规模娇小,prim随意处理即合..........

代码:

代码语言:javascript
复制
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int maxn=105;
 8 double map[maxn][maxn];
 9 double lowc[maxn];
10 bool vis[maxn];
11 struct point{
12 
13   double x,y;
14   int id;
15 };
16 
17 inline double dista(point a,point b){
18   return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));
19 }
20 
21 point sac[maxn];
22 
23 double prim(int st,int n)
24 {
25     memset(vis,0,sizeof(vis));
26     vis[st]=true;
27     double minc=inf;
28     for(int i=1;i<=n;i++)
29         lowc[i]=map[st][i];
30     int pre=st;
31     double sum=0.0;
32     for(int i=1;i<n;i++){
33         minc=inf;
34         for(int j=1;j<=n;j++)
35         {
36             if(!vis[j]&&minc>lowc[j]){
37                 minc=lowc[j];
38                 pre=j;
39             }
40         }
41          sum+=minc;
42          vis[pre]=true;
43         for(int j=1;j<=n;j++){
44             if(!vis[j]&&lowc[j]>map[pre][j]){
45                 lowc[j]=map[pre][j];
46             }
47         }
48     }
49     return sum;
50 }
51 void work(int n)
52 {
53     for(int i=0;i<n;i++){
54         for(int j=0;j<n;j++){
55           map[i+1][j+1]=map[j+1][i+1]=dista(sac[i],sac[j]);
56         }
57     }
58 }
59 int  main(){
60    int n;
61    while(scanf("%d",&n)!=EOF){
62 
63       for(int i=0;i<n;i++)
64       {
65         scanf("%lf%lf",&sac[i].x,&sac[i].y);
66         sac[i].id=i+1;
67       }
68      work(n);
69      printf("%.2lf\n",prim(1,n));
70    }
71    return 0;
72 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-08-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Eddy's picture
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档