原题地址:http://poj.org/problem?id=2560
Freckles
Time Limit: 1000MS |
Memory Limit: 65536K |
|
Total Submissions: 7863 |
Accepted: 3776 |
Description
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 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
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
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
题意:给你n个直角坐标系的点,求最小生成树的权重和。
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 const int N = 110, Infinity = 0x3f3f3f3f; 5 struct Point 6 { 7 double x; 8 double y; 9 }vertex[N]; 10 double distance[N][N], D[N]; 11 bool cnt[N] = {0}; 12 int main(void) 13 { 14 int n, start, mintag; 15 double sum, x, y; 16 while(~scanf("%d", &n)) 17 { 18 for(int i=0; i<n; ++i) 19 scanf("%lf %lf", &vertex[i].x, &vertex[i].y); 20 for(int i=0; i<n; ++i) 21 { 22 distance[i][i] = Infinity; 23 for(int j=i+1; j<n; ++j) 24 { 25 x = vertex[i].x - vertex[j].x; 26 y = vertex[i].y - vertex[j].y; 27 distance[i][j] = sqrt(x*x + y*y); 28 distance[j][i] = distance[i][j]; 29 } 30 } 31 sum = 0; 32 start = 0; 33 cnt[0] = true; 34 memcpy(D, distance[0], sizeof(D)); 35 for(int i=1; i<n; ++i) 36 { 37 for(int j=1; j<n; ++j) 38 { 39 if(!cnt[j] && distance[start][j] < D[j]) 40 D[j] = distance[start][j]; 41 } 42 mintag = 0; 43 for(int j=1; j<n; ++j) 44 { 45 if(cnt[j]) 46 continue; 47 if(D[j] < D[mintag]) 48 mintag = j; 49 } 50 cnt[mintag] = true; 51 sum += D[mintag]; 52 start = mintag; 53 } 54 memset(cnt, 0, sizeof(cnt)); 55 printf("%.2lf\n", sum); 56 } 57 }