题目链接:http://openoj.awaysoft.com:8080/judge/contest/view.action?cid=384#problem/A
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 double dis[110][110];
5 int vist[110];
6
7 struct node
8 {
9 double x,y;
10 }p[105];
11
12 double map(node a,node b)
13 {
14 return ( sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) ) );
15 }
16
17 int main()
18 {
19 int t=1,i,j,n,T;
20 double s;
21 while (~scanf("%d",&n) && n)
22 {
23 if (t!=1)
24 printf("\n");
25 for (i=0;i<n;i++)
26 scanf("%lf%lf",&p[i].x,&p[i].y);
27 memset(vist,0,sizeof(vist));
28 for (i=0;i<n-1;i++)
29 for (j=i+1;j<n;j++)
30 dis[i][j]=dis[j][i]=map(p[i],p[j]);
31 vist[0]=1;
32 T=n-1;
33 s=0;
34 while (T--)
35 {
36 double m=10010;
37 int pos;
38 for (i=1;i<n;i++)
39 if (!vist[i]&&dis[0][i]<m)
40 {
41 m=dis[0][i];
42 pos=i;
43 }
44 vist[pos]=1;
45 s+=m;
46 for (i=1;i<n;i++)
47 if (!vist[i]&&dis[pos][i]<dis[0][i])
48 dis[0][i]=dis[pos][i];
49 }
50 printf("Case #%d:\n",t++);
51 printf("The minimal distance is: %0.2lf\n",s);
52 }
53 return 0;
54 }
2 #include <string.h>
3 #include <math.h>
4 double dis[110][110];
5 int vist[110];
6
7 struct node
8 {
9 double x,y;
10 }p[105];
11
12 double map(node a,node b)
13 {
14 return ( sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) ) );
15 }
16
17 int main()
18 {
19 int t=1,i,j,n,T;
20 double s;
21 while (~scanf("%d",&n) && n)
22 {
23 if (t!=1)
24 printf("\n");
25 for (i=0;i<n;i++)
26 scanf("%lf%lf",&p[i].x,&p[i].y);
27 memset(vist,0,sizeof(vist));
28 for (i=0;i<n-1;i++)
29 for (j=i+1;j<n;j++)
30 dis[i][j]=dis[j][i]=map(p[i],p[j]);
31 vist[0]=1;
32 T=n-1;
33 s=0;
34 while (T--)
35 {
36 double m=10010;
37 int pos;
38 for (i=1;i<n;i++)
39 if (!vist[i]&&dis[0][i]<m)
40 {
41 m=dis[0][i];
42 pos=i;
43 }
44 vist[pos]=1;
45 s+=m;
46 for (i=1;i<n;i++)
47 if (!vist[i]&&dis[pos][i]<dis[0][i])
48 dis[0][i]=dis[pos][i];
49 }
50 printf("Case #%d:\n",t++);
51 printf("The minimal distance is: %0.2lf\n",s);
52 }
53 return 0;
54 }