HDU 1875 畅通工程再续

相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组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<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<string.h>
 5 #include<algorithm>
 6 #define LL long long
 7 #define N 100001 
 8 using namespace std;
 9 int pre[N];
10 struct node{
11     int a, b;
12     double w;
13 }road[N];
14 int fi(int x) {
15     return pre[x]==x?x:pre[x]=fi(pre[x]);
16 }
17 
18 void uni(int x, int y){
19     int fx=fi(x), fy=fi(y);
20     if(fx!=fy){
21         pre[fx]=fy;
22     }
23 }
24 
25 bool cmp(node x, node y){
26     return x.w<y.w;
27 }
28 
29 int main(){
30     int n, m, t,x[N],y[N];
31     cin>>t;
32     while(t--){
33         cin>>n;
34         for(int i=1; i<=n; i++) {
35             pre[i]=i;
36         }
37         for(int i=1; i<=n; i++){
38             cin>>x[i]>>y[i];
39         }
40         int cnt=0;
41         for(int i=1; i<=n; i++){
42             for(int j=i+1; j<=n; j++){
43                 double t=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
44                 if(t<10||t>1000) continue;
45                 road[cnt].a=i, road[cnt].b=j;
46                 road[cnt++].w=t;
47             }
48         }
49         sort(road, road+cnt, cmp);
50         double sum=0;
51         for(int i=0; i<cnt; i++){
52             if(fi(road[i].a)!=fi(road[i].b)){
53                 sum+=road[i].w;
54                 uni(road[i].a, road[i].b);
55             }
56         }
57         int flag=fi(1);
58         int ff=0;
59         for(int i=2; i<=n; i++){
60             if(flag!=fi(i)){
61                 cout<<"oh!"<<endl;
62                 ff=1;
63                 break;
64             }
65         }
66         if(ff) continue;
67         printf("%.1f
",sum*100);
68     }
69 }

(不知道说什么,我搞了半天,想为什么我会输出两位小数,试试总多一位,然后……哦~我没有换行)

原文地址:https://www.cnblogs.com/cake-lover-77/p/10911818.html