• hdu1875 畅通工程再续 最小生成树并查集解决---kruskal


     http://acm.hdu.edu.cn/showproblem.php?pid=1875

    畅通工程再续

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 17737    Accepted Submission(s): 5536


    Problem 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!
     

    Author
    8600
     

    Source
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1879 1102 1301 1162 1856 
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 int k,c,u[10005],v[10005],sum,r[10005],p[200],a[200][2];
     8 double w[10005],cnt;
     9 int dd(int a,int b,int c,int d);
    10 int kruskal();
    11 int main()
    12 {
    13     int t;
    14     scanf("%d",&t);
    15     while(t--)
    16     {k=0;
    17         scanf("%d",&c);
    18         for(int i=0;i<c;i++)
    19         {
    20             scanf("%d%d",&a[i][0],&a[i][1]);
    21             for(int j=0;j<i;j++)
    22             {
    23                 if(dd(a[i][0],a[i][1],a[j][0],a[j][1])<=1000000&&dd(a[i][0],a[i][1],a[j][0],a[j][1])>=100)
    24                 {
    25                     u[k]=i;
    26                     v[k]=j;
    27                     w[k]=sqrt((double)(dd(a[i][0],a[i][1],a[j][0],a[j][1])));
    28                     //cout<<i<<"***"<<j<<"***"<<k<<"***"<<w[k]<<"
    
    
    ";
    29                     //cout<<dd(1,1,2,2)<<"*&*&";
    30                     k++;
    31                 }
    32             }
    33         }kruskal();
    34     }return 0;
    35 }
    36 int dd(int  a,int b,int c,int d){return ((a-c)*(a-c)+(b-d)*(b-d));}
    37 int cmp(int i,int j){return w[i]<w[j];}
    38 int find(int x){return x==p[x]?x:(p[x]=find(p[x])); }
    39 
    40 int kruskal()
    41 {sum=0;cnt=0;
    42     //cout<<k<<"***
    ";
    43     for(int i=0;i<c;i++)p[i]=i;
    44     for(int i=0;i<k;i++)r[i]=i;
    45     sort(r,r+k,cmp);
    46     for(int i=0;i<k;i++)
    47     {
    48         int e=r[i];
    49         int x=find(u[e]),y=find(v[e]);
    50         if(x!=y){sum++;cnt+=w[e];p[x]=y;}
    51 
    52     }
    53     if(sum==c-1)printf("%.1lf
    ",cnt*100.0);
    54     else printf("oh!
    ");
    55     return 0;
    56 }
  • 相关阅读:
    Licp
    [持续更新]Python 笔记
    求 LCA 的三种方法
    一些题目(4)
    欧几里得算法与扩展欧几里得算法
    最近打的三场比赛的总结
    10day2
    10day1
    chrome谷歌浏览器插件制作简易教程
    nodejs net模块
  • 原文地址:https://www.cnblogs.com/SSYYGAM/p/4526113.html
Copyright © 2020-2023  润新知