• 畅通工程再续


    Description

    相 信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展 首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓 符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
     

    Input

    输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。 每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
     

    Output

    每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
     

    Sample Input

    2 2 10 10 20 20 3 1 1 2 2 1000 1000
     

    Sample Output

    1414.2 oh!
     
     
    View Code
     1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<math.h>
    4 #include <algorithm>
    5 using namespace std;
    6 double xx[1000], yy[1000];
    7 struct node
    8 {
    9 int i, j;
    10 double val;
    11 }T[100000];
    12 bool cmp( node A, node B )
    13 {
    14 return A.val < B.val;
    15 }
    16 int set[1000],n;
    17 void make_set()
    18 {
    19 for( int i = 1; i < 1000; i++ )
    20 set[i] = i;
    21 }
    22 int find( int x )
    23 {
    24 return x == set[x]?x:set[x] = find(set[x]);
    25 }
    26 int merge( int x, int y )
    27 {
    28 int x1 = find(x), y1 = find(y);
    29 if( x1 != y1 )
    30 set[x1] = y1;
    31 }
    32 double krusal( int count )
    33 {
    34 sort( T, T+count, cmp );
    35 int a, b;
    36 double result = 0, c;
    37 int oo = 0;
    38 for( int i = 0; i < count; i++ )
    39 {
    40 a = T[i].i;
    41 b = T[i].j;
    42 c = T[i].val;
    43 if( find(a) != find(b) )
    44 {
    45
    46 if( c >= 10 && c <= 1000)
    47 {
    48 merge(a , b);
    49 result += c;
    50 oo++;
    51 }
    52 }
    53 }
    54 if( oo == n-1 )
    55 printf( "%.1lf\n", result*100 );
    56 else
    57 printf( "oh!\n" );
    58 }
    59 int main()
    60 {
    61 int N;
    62 scanf( "%d", &N );
    63 while( N-- )
    64 {
    65 int count = 0;
    66 make_set();
    67 scanf( "%d", &n );
    68 for( int i = 1; i <= n; i++ )
    69 scanf( "%lf%lf", &xx[i], &yy[i] );
    70 for( int i = 1; i <= n; i++ )
    71 {
    72 for( int j = i+1; j <= n; j++ )
    73 {
    74 T[count].i = i;
    75 T[count].j = j;
    76 T[count].val = sqrt( (xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
    77 count++;
    78 }
    79 }
    80 krusal( count );
    81 }
    82
    83 }
  • 相关阅读:
    HomeWork2
    An error I have completed recently
    C#之规格说明书
    App上架审核指南翻译
    使用CollectionView做横向滑动分页效果:
    推荐一些CSS命名规范
    关于让左右2个DIV高度相等
    带有缩略图和文字提示的轮播图
    动画的定义:
    .Net基础篇_学习笔记_第五天_流程控制while循环002
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2405019.html
Copyright © 2020-2023  润新知