Problem A: Freckles
In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
Sample Input
1 3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
给一个二维平面上的一些点,让你在这些点间连一些直线,使得所有的点都能连通且所画的线段总长度最小
典型的最小生成树问题,用Kruskal算法直接写即可
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 7 using namespace std; 8 9 typedef struct 10 { 11 double x; 12 double y; 13 } NODE; 14 15 typedef struct 16 { 17 int a; 18 int b; 19 double len; 20 } EDGE; 21 22 int n; 23 int father[10050]; 24 NODE v[150]; 25 EDGE e[10050]; 26 27 double dis(NODE a,NODE b) 28 { 29 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 30 } 31 32 bool cmp(EDGE a,EDGE b) 33 { 34 return a.len<b.len; 35 } 36 37 int find_father(int w) 38 { 39 if(father[w]==w) 40 return w; 41 return father[w]=find_father(father[w]); 42 } 43 44 bool same(int x,int y) 45 { 46 int root_x=find_father(x); 47 int root_y=find_father(y); 48 return root_x==root_y; 49 } 50 51 void Union(int x,int y) 52 { 53 if(!same(x,y)) 54 father[father[y]]=father[x]; 55 } 56 57 int main() 58 { 59 int kase; 60 61 scanf("%d",&kase); 62 63 while(kase--) 64 { 65 scanf("%d",&n); 66 67 for(int i=1;i<=n;i++) 68 { 69 scanf("%lf %lf",&v[i].x,&v[i].y); 70 for(int j=1;j<i;j++) 71 if(v[i].x==v[j].x&&v[i].y==v[j].y) 72 { 73 i--; 74 break; 75 } 76 } 77 78 int t=0; 79 80 for(int i=1;i<=n;i++) 81 for(int j=i+1;j<=n;j++) 82 { 83 e[t].a=i; 84 e[t].b=j; 85 e[t].len=dis(v[i],v[j]); 86 t++; 87 } 88 89 sort(e,e+t,cmp); 90 91 for(int i=0;i<=n;i++) 92 father[i]=i; 93 94 double ans=0; 95 96 for(int i=0;i<t;i++) 97 { 98 if(!same(e[i].a,e[i].b)) 99 { 100 Union(e[i].a,e[i].b); 101 ans+=e[i].len; 102 } 103 } 104 105 printf("%.2f\n",ans); 106 107 if(kase) 108 puts(""); 109 } 110 111 return 0; 112 }