暢通工程再續(xù)
Time Limit: 2000/1000 MS (java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26109 Accepted Submission(s): 8459
PRoblem Description 相信大家都聽說一個(gè)“百島湖”的地方吧,百島湖的居民生活在不同的小島中,當(dāng)他們想去其他的小島時(shí)都要通過劃小船來實(shí)現(xiàn)。現(xiàn)在政府決定大力發(fā)展百島湖,發(fā)展首先要解決的問題當(dāng)然是交通問題,政府決定實(shí)現(xiàn)百島湖的全暢通!經(jīng)過考察小組RPRush對(duì)百島湖的情況充分了解后,決定在符合條件的小島間建上橋,所謂符合條件,就是2個(gè)小島之間的距離不能小于10米,也不能大于1000米。當(dāng)然,為了節(jié)省資金,只要求實(shí)現(xiàn)任意2個(gè)小島之間有路通即可。其中橋的價(jià)格為 100元/米。
Input 輸入包括多組數(shù)據(jù)。輸入首先包括一個(gè)整數(shù)T(T <= 200),代表有T組數(shù)據(jù)。 每組數(shù)據(jù)首先是一個(gè)整數(shù)C(C <= 100),代表小島的個(gè)數(shù),接下來是C組坐標(biāo),代表每個(gè)小島的坐標(biāo),這些坐標(biāo)都是 0 <= x, y <= 1000的整數(shù)。
Output 每組輸入數(shù)據(jù)輸出一行,代表建橋的最小花費(fèi),結(jié)果保留一位小數(shù)。如果無法實(shí)現(xiàn)工程以達(dá)到全部暢通,輸出”oh!”.
Sample Input 2 2 10 10 20 20 3 1 1 2 2 1000 1000
Sample Output 1414.2 oh!
分析 有n個(gè)點(diǎ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"); }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注