• Eddy's picture


    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
     
     
     
    View Code
     1 #include<stdio.h>
    2 #include<math.h>
    3 #include <algorithm>
    4 using namespace std;
    5
    6 struct node
    7 {
    8 int i, j;
    9 double val;
    10 }T[100000];
    11
    12 double xx[350], yy[350];
    13
    14 bool cmp( node A, node B )
    15 {
    16 return A.val < B.val;
    17 }
    18
    19 int set[350];
    20
    21 void make_set( )
    22 {
    23 for( int i = 1; i < 333; i++ )
    24 set[i] = i;
    25 }
    26
    27 int find( int x )
    28 {
    29 return set[x] == x?set[x]:set[x] = find(set[x]);
    30 }
    31
    32 int merge( int x, int y )
    33 {
    34 int x1 = find(x);
    35 int y1 = find(y);
    36 if( x1 != y1 )
    37 set[x1] = y1;
    38 }
    39
    40 double krusal( int cnt )
    41 {
    42 sort( T, T+cnt, cmp );
    43 int a, b;
    44 double c, result = 0;
    45 for( int i = 0; i < cnt; i++ )
    46 {
    47 a = T[i].i;
    48 b = T[i].j;
    49 c = T[i].val;
    50 if( find(a) != find(b) )
    51 {
    52 merge( a, b );
    53 result += c;
    54 }
    55 }
    56 return result;
    57 }
    58 int main()
    59 {
    60 int n;
    61 while( scanf( "%d", &n) == 1 )
    62 {
    63 int cnt = 0;
    64 make_set();
    65 for( int i = 1; i <= n; i++ )
    66 scanf( "%lf%lf", &xx[i], &yy[i] );
    67 for( int i = 1; i <= n; i++ )
    68 {
    69 for( int j = i+1; j <= n; j++ )
    70 {
    71 T[cnt].i = i;
    72 T[cnt].j = j;
    73 T[cnt].val = sqrt( (xx[i]-xx[j])*(xx[i] - xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
    74 cnt++;
    75 }
    76 }
    77 printf( "%.2lf\n",krusal( cnt ) );
    78 }
    79 }
  • 相关阅读:
    Linux Socket函数close() 与 shutdown()区别
    Android Performance Patterns S01E03
    Android Performance Patterns S01E02
    Android Performance Patterns S01E01
    Java类初始化顺序
    原子性,可见性,有序性
    UML类图
    Linux 五种IO模型
    Linux学习笔记(一)
    线程的生命周期-java
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2405023.html
Copyright © 2020-2023  润新知