zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    自学Python3.5-字符串格式化 作用域 递归
    自学Python3.2-函数分类(内置函数)
    自学Python3.1-函数基础
    自学Python2.7-collections系列
    自学Python2.6-深浅拷贝
    自学Python2.5-基本数据类型-set集合
    自学Python2.4-基本数据类型-字典dict(objct)
    自学Python2.3-基本数据类型-元组tuple(object) 方法
    自学Python2.2-基本数据类型-列表list(object)
    java通过jdbc访问mysql,update数据返回值的思考
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2405023.html
Copyright © 2011-2022 走看看