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!
 
思路:最小生成树+并查集
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<math.h>
 6 #include<algorithm>
 7 #define LL long long
 8 #define mod 1e9 + 7
 9 const int M = 105;
10 
11 using namespace std;
12 
13 struct node{
14     int x;
15     int y;
16     double value;
17 }a[M * M >> 1];
18 
19 int cun[M];
20 
21 double dis(int x1, int y1, int x2, int y2)
22 {
23     return sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));
24 }
25 
26 bool cmp(node a, node b)
27 {
28     return a.value < b.value;
29 }
30 
31 int find(int x)
32 {
33     return cun[x] == x ? x : cun[x] = find(cun[x]);
34 }
35 
36 void shu(int x, int y)
37 {
38     int p = find(x);
39     int q = find(y);
40     if(p != q)
41         cun[p] = q;
42 }
43 
44 int main()
45 {
46     int t, n;
47     int s[M], e[M];
48     cin >> t;
49     while( t-- )
50     {
51         cin >> n;
52         for(int i = 1; i <= n; ++i)
53             cin >> s[i] >> e[i];
54         int temp = 1;
55         for(int i = 1; i <= n; ++i)
56             for(int j = i + 1; j <= n; ++j)
57             {
58                 a[temp].x = i;
59                 a[temp].y = j;
60                 a[temp].value = dis(s[i],e[i],s[j],e[j]);
61                 temp++;
62             }
63         for(int i = 0; i <= n; ++i)
64             cun[i] = i;
65         sort(a,a + temp,cmp);
66         int cnt = 1;
67         double ans = 0;
68         for(int i = 1; i < temp; ++i)
69         {
70             if(find(a[i].x) != find(a[i].y) && a[i].value >= 10 && a[i].value <= 1000)
71             {
72                 cnt++;
73                 shu(a[i].x,a[i].y);
74                 ans += a[i].value;
75             }
76         }
77         ans *= 100;
78         if(cnt == n)
79             printf("%.1lf\n",ans);
80         else
81             puts("oh!");
82     }
83     return 0;
84 }
View Code

 

posted on 2014-08-06 10:32  Unico  阅读(118)  评论(0)    收藏  举报