【基础最小生成树】Eddy's picture

Eddy热爱画画并希望改善技艺以获得朋友的认可。为此,他提出了一个有趣的数学问题:如何用最少的墨水长度将纸上的多个点通过直线相连。此问题涉及到计算几何和图论中的最小生成树算法。

Eddy's picture

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


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
 代码:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #define MAX 1008
 6 using namespace std;
 7 struct poin
 8 {
 9     int x;
10     int y;
11     double s;
12 }Dic[100100];
13 
14 int cmp(poin a,poin b)
15 {
16     return a.s<b.s;
17 }
18 int ID[MAX];
19 int Sign;
20 double X[MAX];
21 double Y[MAX];
22 double Map[MAX][MAX];
23 double DIC(double x,double y)
24 {
25     return sqrt(x*x+y*y);
26 }
27 void Cread(int N)
28 {
29     int i,j;
30     Sign=0;
31     for(i=0;i<=N;i++)
32     {
33         ID[i]=i;
34         for(j=0;j<=N;j++)
35             Map[i][j]=MAX;
36     }
37 }
38 
39 int Find(int x)
40 {
41     int TMD;
42     if(x==ID[x])return x;
43     else ID[x]=Find(ID[x]);
44     return ID[x];
45 }
46 
47 void Update(int a,int b)
48 {
49     int A,B;
50     A=Find(a);B=Find(b);
51     if(A!=B)ID[A]=B;
52     else Sign++;
53 }
54 int main()
55 {
56     int T,N,i,j,k,A,B,C,t=1;
57     double X[MAX],Y[MAX],c;
58     while(scanf("%d",&N)!=EOF)
59     {
60         Cread(N);
61         for(i=0;i<N;i++)
62         {
63             scanf(" %lf %lf",&X[i],&Y[i]);
64         }
65         for(i=0;i<N;i++)
66         {
67             for(j=0;j<N;j++)
68             {
69                 Dic[i*N+j].x=i;Dic[i*N+j].y=j;
70                 if(i==j){Dic[i*N+j].s=MAX;}
71                 c=DIC(X[i]-X[j],Y[i]-Y[j]);
72                 Dic[i*N+j].s=c;
73             }
74         }
75         sort(Dic,Dic+N*N,cmp);
76         double Sum=0;
77         int Sign=0;
78         for(k=0;k<N*N;k++)
79         {
80             if(fabs(Dic[k].s-MAX)<=0.00001){Sign=0;break;}
81             else
82             {
83                 A=Find(Dic[k].x);B=Find(Dic[k].y);
84                 if(A!=B)
85                 {
86                     ID[A]=B;
87                     Sum+=Dic[k].s;
88                     Sign++;
89                 }
90             }
91         }
92        printf("%.2lf\n",Sum);
93     }
94     return 0;
95 }
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/4735770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值