暢通工程再續
Time Limit: 2000/1000 MS (java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26109 Accepted Submission(s): 8459
PRoblem Description 相信大家都聽說一個“百島湖”的地方吧,百島湖的居民生活在不同的小島中,當他們想去其他的小島時都要通過劃小船來實現?,F在政府決定大力發展百島湖,發展首先要解決的問題當然是交通問題,政府決定實現百島湖的全暢通!經過考察小組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!
分析 有n個點,則有 n*(n-1) 條邊,然后kruskal
#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>#include<cmath>using namespace std;const int maxn=110;int fa[maxn];void init(){ for(int i=0;i<maxn;i++) fa[i]=i;}int Find(int x){ if(fa[x] == x) return fa[x]; else return fa[x]= Find(fa[x]);}void Union(int x,int y){ int fx=Find(x),fy=Find(y); if(fx != fy) fa[fx] =fy;}typedef struct{ int st,ed; double cost;}Edge;Edge edge[10005];int cmp(Edge a,Edge b){ return a.cost <b.cost;}int X[110];int Y[110];int main(){ int T; cin>>T; while(T--){ int n; cin>>n; init(); for(int i=0;i<n;i++) cin>>X[i]>>Y[i]; int m=0; for(int i=0;i <n;i++) for(int j=0;j< n;j++){ if( i== j) continue; edge[m].st=i; edge[m].ed=j; edge[m].cost=sqrt( pow( X[i]-X[j],2)+pow(Y[i]-Y[j],2) ); // cout<<"cost="<<edge[m].cost<<endl; m++; } // cout<<"m="<<m<<endl; sort(edge,edge+m,cmp); int rst=n; double tot_cost=0; for(int i=0;i<m && rst>1;i++){ if(Find(edge[i].st)!= Find(edge[i].ed) && (edge[i].cost>=10 && edge[i].cost <=1000)){ Union(edge[i].st,edge[i].ed); rst--; tot_cost +=edge[i].cost; } } // cout<<"rst="<<rst<<endl; if(rst==1) printf("%.1f/n",tot_cost*100); else printf("oh!/n"); }}新聞熱點
疑難解答