Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1554 Accepted Submission(s): 677
本题非常简单,最小生成树问题,直接抄模板即可
代码:
1 #include<stdio.h>
2 #include<math.h>
3 struct node
4 {
5 double x,y;
6 }s[105];
7 int n;
8 double prim(int v0)
9 {
10 int i,j,visit[105],u;
11 double sum=0,min,distance[105],s1;
12 for(i=1;i<=n;i++)
13 {
14 distance[i]=sqrt((s[v0].x-s[i].x)*(s[v0].x-s[i].x)+(s[v0].y-s[i].y)*(s[v0].y-s[i].y));
15 visit[i]=0;
16 }
17 visit[v0]=1;
18 for(i=1;i<n;i++)
19 {
20 min=0xfffffff;
21 for(j=1;j<=n;j++)
22 {
23 if(!visit[j] && distance[j]<min)
24 {
25 u=j;
26 min=distance[j];
27 }
28 }
29 sum+=min;
30 visit[u]=1;
31 for(j=1;j<=n;j++)
32 {
33 s1=sqrt((s[u].x-s[j].x)*(s[u].x-s[j].x)+(s[u].y-s[j].y)*(s[u].y-s[j].y));
34 if(!visit[j] && s1<distance[j])
35 distance[j]=s1;
36 }
37 }
38 return sum;
39 }
40 int main()
41 {
42 int i;double x,y;
43 while(scanf("%d",&n)!=EOF)
44 {
45 for(i=1;i<=n;i++)
46 {
47 scanf("%lf%lf",&x,&y);
48 s[i].x=x;
49 s[i].y=y;
50 }
51 printf("%.2lf\n",prim(1));
52 }
53 return 0;
54 }