zoukankan      html  css  js  c++  java
  • 畅通工程再续

    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组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
      1 #include <iostream>
      2 using namespace std;
      3 #include<string.h>
      4 #include<set>
      5 #include<stdio.h>
      6 #include<math.h>
      7 #include<queue>
      8 #include<map>
      9 #include<algorithm>
     10 #include<cstdio>
     11 #include<cmath>
     12 #include<cstring>
     13 #include <cstdio>
     14 #include <cstdlib>
     15 #include<cstring>
     16 int n;
     17 int a[110];
     18 int lenTM;
     19 struct lll
     20 {
     21     double x,y;
     22 }qqq[110];
     23 struct aaa
     24 {
     25     int x,y;
     26     double len;
     27 }TM[110];
     28 int cmp(aaa a,aaa b)
     29 {
     30     return a.len<b.len;
     31 }
     32 int find(int n)
     33 {
     34     if(a[n]==n)
     35         return n;
     36     else
     37         return a[n]=find(a[n]);
     38 }
     39 int  duzhi(int n,int m)
     40 {
     41     int n1=find(n);
     42     int m1=find(m);
     43     if(n1!=m1)
     44     {
     45         a[m1]=n1;
     46         return 0;
     47     }
     48     else
     49         return 1;
     50 }
     51 struct cmp1{
     52     bool operator ()(aaa &a,aaa &b){
     53         return a.len>b.len;//最小值优先
     54     }
     55 };
     56 priority_queue<aaa,vector<aaa>,cmp1>s;
     57 int main()
     58 {
     59     int t;
     60     cin>>t;
     61     while(t--)
     62     {
     63         memset(a,0,sizeof(a));
     64         memset(qqq,0,sizeof(qqq));
     65         memset(TM,0,sizeof(TM));
     66         cin>>n;
     67         for(int i=1;i<=n;i++)
     68         {
     69             cin>>qqq[i].x>>qqq[i].y;
     70             a[i]=i;
     71         }
     72         lenTM=0;
     73         for(int i=1;i<=n;i++)
     74         {
     75             for(int j=1;j<=n;j++)
     76             {
     77                 if(i==j)
     78                     continue;
     79                 TM[lenTM].x=i;
     80                 TM[lenTM].y=j;
     81                 TM[lenTM].len=sqrt(pow(qqq[i].x-qqq[j].x,2)+pow(qqq[i].y-qqq[j].y,2));
     82                 s.push(TM[lenTM]);
     83             }
     84         }
     85         double sum=0;
     86         while(!s.empty())
     87         {
     88             if(s.top().len<10||s.top().len>1000)
     89             {
     90                 s.pop();
     91             }
     92             else if(duzhi(s.top().x,s.top().y))
     93             {
     94                 s.pop();
     95             }
     96             else
     97             {
     98                 sum+=s.top().len;
     99                 s.pop();
    100             }
    101 
    102         }
    103         int add=0;
    104         for(int i=1;i<=n;i++)
    105             {
    106                 if(a[i]==i)
    107                     add++;
    108             }
    109             if(add==1)
    110                 printf("%.1lf
    ",sum*100);
    111         else
    112             cout<<"oh!"<<endl;
    113     }
    114     return 0;
    115 }
    View Code
    2 2
    1000 1000

    Sample Output

    1414.2
    oh!
    最小生成树 队列加并查集
  • 相关阅读:
    5M1E,软件质量管理最佳解决方案
    Laravel 刚创建的项目 API 无法使用 Session,是没有在Api的请求内开启Session
    python添加默认模块搜索路径
    [传纸条]
    [miller_rabin]
    国内rog游戏手机2与国际版本的 差别
    windows下依端口查进程
    gor phone 2 优缺点
    IDA pro 6.8 导入cpu解析模块报错 ImportError: No module named ida_ bytes
    JEB 4.5 新增 RISC-V 反编译器支持
  • 原文地址:https://www.cnblogs.com/dulute/p/7295572.html
Copyright © 2011-2022 走看看