zoukankan      html  css  js  c++  java
  • hdu1875 畅通工程再续 并查集/最小生成树

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

    题意:给出若干个点,若两点之间距离大于等于10且小于等于1000即有边,边的费用是长度*100,问是否能连通所有点,若能,最小花费是多少。

    最小生成树裸题

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<math.h>
     5 using namespace std;
     6 
     7 struct seg{
     8     int a,b;
     9     double l;
    10     bool operator <(const seg a)const{
    11         return l-a.l<1e-10;
    12     }
    13 }s[5000];
    14 
    15 struct point{
    16     int x,y;
    17 }p[105];
    18 
    19 int n,fa[105];
    20 
    21 void init(){
    22     for(int i=1;i<=n;i++)fa[i]=i;
    23 }
    24 
    25 int find(int x){
    26     return x==fa[x]?x:fa[x]=find(fa[x]);
    27 }
    28 
    29 int main(){
    30     int T;
    31     while(scanf("%d",&T)!=EOF){
    32         for(int q=1;q<=T;q++){
    33             scanf("%d",&n);
    34             int i;
    35             for(i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
    36             int j,c=0;
    37             for(i=1;i<=n;i++){
    38                 for(j=i+1;j<=n;j++){
    39                     double l=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)*1.0+(p[i].y-p[j].y)*(p[i].y-p[j].y)*1.0);
    40                     if(l>=10&&l<=1000){
    41                         s[++c].a=i;
    42                         s[c].b=j;
    43                         s[c].l=l;
    44                     }
    45                 }
    46             }
    47             init();
    48             sort(s+1,s+c+1);
    49             bool f=0;
    50             int t=0;
    51             double ans=0;
    52             if(t==n-1)f=1;
    53             for(i=1;i<=c;i++){
    54                 int x=find(s[i].a),y=find(s[i].b);
    55                 if(x!=y){
    56                     fa[x]=y;
    57                     t++;
    58                     ans+=s[i].l;
    59                     if(t==n-1){
    60                         f=1;
    61                         break;
    62                     }
    63                 }
    64             }
    65             if(f) printf("%.1lf
    ",ans*100);
    66             else printf("oh!
    ");
    67         }
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    矩阵快速幂模板
    POJ 3761 Bubble Sort 快速幂取模+组合数学
    MySQL批量修改表前缀
    js生成条形码插件
    如何将本地代码通过git上传到码云
    jQuery常用方法
    MySQL按日、周、月统计数据
    PHP文件下载
    python报错ModelNotFoundError
    thinkphp生成的验证码提示因存在错误无法显示
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592335.html
Copyright © 2011-2022 走看看