【杭电oj】1875 - 畅通工程再续(克鲁斯塔尔)

来源:互联网 发布:维基百科数据库 编辑:程序博客网 时间:2024/05/24 01:39

畅通工程再续

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21945    Accepted Submission(s): 6942


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
2210 1020 2031 12 21000 1000
 

Sample Output
1414.2oh!
 

Author
8600
 

Source
2008浙大研究生复试热身赛(2)——全真模拟
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1874 1272 1301 1856 1213 


水过,代码如下:

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;int f[111];struct node1{int st,end;double cost;}road[5111];struct node2{double x,y;}data[111];int find(int x){if (x!=f[x])f[x]=find(f[x]);return f[x];}int join (int x,int y){int fx,fy;fx=find (x);fy=find (y);if (fx!=fy){f[fx]=fy;return 1;}return 0;}bool cmp(node1 a,node1 b){return a.cost<b.cost;}double dis(node2 a,node2 b){double ans;ans=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));return ans;}int main(){int n;int u;double ans;scanf ("%d",&u);while (u--){scanf ("%d",&n);ans=0;for (int i=1;i<=n;i++)f[i]=i;//别忘了初始化 for (int i=1;i<=n;i++)scanf ("%lf %lf",&data[i].x,&data[i].y);int num=-1;for (int i=1;i<n;i++){for (int j=i+1;j<=n;j++){double t;t=dis(data[i],data[j]);if (t>=10 && t<=1000){num++;road[num].st=i;road[num].end=j;road[num].cost=t;}}}sort (road,road+num+1,cmp);//这里注意num要 +1 int ach=0;//已经建成的桥数 for (int i=0;i<=num;i++){if (join(road[i].st,road[i].end)){ans+=road[i].cost;ach++;}}ans*=100;if (ach==n-1)printf ("%.1lf\n",ans);elseprintf ("oh!\n");}return 0;}


0 0
原创粉丝点击