B

相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组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!

解题思路:还是最小生成树的问题,不过我的代码还是太长了,过段时间优化一下;

  1 #include <iostream>
  2 #include <queue>
  3 #include <string.h>
  4 #include <stdio.h>
  5 #include <math.h>
  6 using namespace std;
  7 
  8 const int MAX = 100 + 20;
  9 int n;
 10 int visit[MAX];
 11 
 12 struct S
 13 {
 14     double x,y;
 15 };S dao[MAX];
 16 
 17 struct T
 18 {
 19     int a,b;
 20     double len;
 21 };
 22 
 23 struct cmp
 24 {
 25     bool operator() (T a,T b)
 26     {
 27         return a.len > b.len;
 28     }
 29 };
 30 
 31 double JS(int i,int j)
 32 {
 33     return sqrt( (dao[i].x-dao[j].x)*(dao[i].x-dao[j].x) +(dao[i].y-dao[j].y)*(dao[i].y-dao[j].y) );
 34 }
 35 
 36 int Find(int x)
 37 {
 38     if(x = visit[x])
 39         return x;
 40     else
 41         return visit[x] = Find(visit[x]);
 42 }
 43 
 44 int mix(int x,int y)
 45 {
 46     int TT = 0;
 47     int Tx = Find(x);
 48     int Ty = Find(y);
 49     if(Tx!=Ty)
 50     {
 51         visit[Tx] = Ty;
 52         TT = 1;
 53     }
 54     return TT;
 55 
 56 }
 57 
 58 
 59 int main()
 60 {
 61     int N;
 62     cin>>N;
 63     while(N--)
 64     {
 65         priority_queue<T,vector<T>,cmp>P;
 66         cin>>n;
 67         for(int i =1;i<=n;i++)
 68             visit[i] = i;
 69 
 70 
 71         for(int i = 1;i <= n;i++)
 72             cin>>dao[i].x>>dao[i].y;
 73 
 74         T temp;
 75         for(int i = 1;i <n;i++)
 76             for(int j = i+1;j<=n;j++)
 77             {
 78                temp.a = i;temp.b = j;
 79                temp.len = JS(i,j);
 80                if(temp.len >=10&&temp.len<=1000)
 81                 P.push(temp);
 82             }
 83 
 84         double sum = 0;
 85         while(!P.empty())
 86         {
 87             temp = P.top();
 88             P.pop();
 89             if( mix(temp.a,temp.b))
 90                 sum+=temp.len;
 91         }
 92         
 93         int ti = 1;
 94         int TTT = Find(1);
 95         for(int i = 2;i <=n;i++)
 96         {
 97             if(Find(i)!=TTT)
 98                 ti++;
 99         }
100 
101         if(ti == 1)
102             printf("%.1lf
",sum*100);
103         else
104             cout<<"oh!"<<endl;
105     }
106 
107 
108     return 0;
109 }
原文地址:https://www.cnblogs.com/a2985812043/p/7294148.html