专题四--1002

题目

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

题目大意

最小生成树的裸题,给你一些点的坐标,然后求连通这些点的最短的线段的长度。注意要将数据处理一下,先求出两个点之间的长度。

AC代码

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<vector>
  6. using namespace std;
  7. const int MAX=105;
  8. struct point {
  9. double x;
  10. double y;
  11. } po [MAX];
  12. struct link {
  13. int x;
  14. int y;
  15. double d;
  16. };
  17. int c[MAX];
  18. double dist(point a,point b)
  19. {
  20. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y*b.y)*(a.y*b.y));
  21. }
  22. int find(int a)
  23. {
  24. while(c[a]!=a)
  25. a=c[a];
  26. return a;
  27. }
  28. void merg(int a,int b)
  29. {
  30. a=find(a);
  31. b=find(b);
  32. if(a!=b)
  33. c[a]=b;
  34. }
  35. bool cmp(link a,link b)
  36. {
  37. return a.d<b.d;
  38. }
  39. int main()
  40. {
  41. //freopen("date.in","r",stdin);
  42. //freopen("date.out","w",stdout);
  43. int n,x,y,i,j,res;
  44. link tem;
  45. vector<link> dis;
  46. while(scanf("%d",&n)) {
  47. res=0;
  48. dis.clear();
  49. for(i=1; i<=n; i++) {
  50. scanf("%lf%lf",&po[i].x,&po[i].y);
  51. for(j=i-1; j>=1; j--) {
  52. tem.x=i;
  53. tem.y=j;
  54. tem.d=dist(po[i],po[j]);
  55. dis.push_back(tem);
  56. }
  57. }
  58. vector<link>::iterator b=dis.begin();
  59. vector<link>::iterator e=dis.end();
  60. sort(b,e,cmp);
  61. for(; b!=e; b++) {
  62. if(find(b->x)!=find(b->y)) {
  63. res+=(b->d);
  64. merg(b->x,b->y);
  65. }
  66. }
  67. printf("%.2lf ",res);
  68. }
  69. }




原文地址:https://www.cnblogs.com/liuzhanshan/p/5648492.html