6.1.2 Eddy's picture

Eddy's picture

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

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 <cstdio>
 2 #include <cstring>   
 3 #include <iostream>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <cstdlib>
 7 #include <queue>
 8 using namespace std;
 9 
10 const int maxn=110,maxm=10100;
11 double t1,t2;
12 double a[maxn][maxn],x[maxn],y[maxn],ans;
13 bool f[maxm];
14 int n,m,point;
15 
16 
17 struct qq
18 {
19     int n;
20     double v;
21     friend bool operator < (qq a,qq b)
22     {
23         return a.v>b.v;
24     }
25 } s;
26 priority_queue<qq> q;
27 void close()
28 {
29     exit(0);
30 }
31 
32 double dist(double x1,double y1,double x2,double y2)
33 {
34     t1=abs(x1-x2);t1*=t1;
35     t2=abs(y1-y2);t2*=t2;
36     return (sqrt(t1+t2));
37 }
38 
39 void prim()
40 {
41     memset(f,false,sizeof(f));
42     while (!q.empty())
43         q.pop();
44     f[1]=true;
45     for (int i=2;i<=n;i++)
46     {
47         s.n=i;
48         s.v=a[i][1];
49         q.push(s);
50     }
51     ans=0;
52     for (int i=1;i<=n-1;i++)
53     {
54         while (1)
55         {
56             s=q.top();
57             q.pop();
58             point=s.n;
59             if (not f[s.n]) break;
60         }
61         ans+=s.v;
62         f[s.n]=true;
63         for (int j=1;j<=n;j++)
64             if (not f[j])
65             {
66                 s.n=j;
67                 s.v=a[j][point];
68                 q.push(s);
69             }
70     }
71     printf("%.2lf\n",ans);
72 }
73 
74 
75 void init()
76 {
77     while (scanf("%d",&n)!=EOF)
78     {
79         for (int i=1;i<=n;i++)
80             scanf("%lf %lf",&x[i],&y[i]);
81         for (int i=1;i<=n;i++)
82             for (int j=1;j<=n;j++)
83                 if (i==j)
84                     a[i][j]=0;
85                 else
86                     a[i][j]=dist(x[i],y[i],x[j],y[j]);
87         prim();
88     }
89 }
90 
91 
92 int main ()
93 {
94     init();
95     close();
96 }
原文地址:https://www.cnblogs.com/cssystem/p/3045956.html