1. 程式人生 > >暢通工程再續 HDU - 1875

暢通工程再續 HDU - 1875

double using AC ble -- mil == 最小 接下來

相信大家都聽說一個“百島湖”的地方吧,百島湖的居民生活在不同的小島中,當他們想去其他的小島時都要通過劃小船來實現。現在政府決定大力發展百島湖,發展首先要解決的問題當然是交通問題,政府決定實現百島湖的全暢通!經過考察小組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!
這題也是水題。最小生成樹的入門題。本菜鳥是通過並查集做的。
算出每兩個島之間的距離然後進行排序,然後特判符合條件的就combine。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 10010
int a[105];
struct node {
    
int x,y; double cost; } qu1[maxn],qu2[maxn]; int find(int r) { while(r!=a[r]) r=a[r]; return r; } int cmp(node a,node b) { return a.cost<b.cost; } int combine(int x ,int y ) { int temp1=find(x); int temp2=find(y); if (temp1==temp2) return 0; if (temp1<temp2) a[temp2]=temp1;
if (temp1>temp2) a[temp1]=temp2; return 1; } int main() { int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); for (int i=0 ;i<n;i++) { scanf("%d%d",&qu1[i].x,&qu1[i].y); } for (int i=1 ;i<=n ;i++) a[i]=i; int k=0; for (int i=0 ;i<n ;i++){ for (int j=0 ;j<i ;j++){ qu2[k].x=i; qu2[k].y=j; qu2[k].cost=sqrt((qu1[i].x-qu1[j].x)*(qu1[i].x-qu1[j].x)+(qu1[i].y-qu1[j].y)*(qu1[i].y-qu1[j].y)); k++; } } int ans=0; double sum=0; sort(qu2,qu2+k,cmp); for (int i=0 ;i<k ;i++){ if (qu2[i].cost>=10 && qu2[i].cost<=1000 && combine(qu2[i].x,qu2[i].y)) { ans++; sum+=qu2[i].cost; } if (ans==n-1) break; } if (ans==n-1) printf("%.1lf\n",sum*100); else printf("oh!\n"); } return 0; }

 

暢通工程再續 HDU - 1875