畅通工程再续
畅通工程再续
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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
2
2
10 10
20 20
3
1 1
2 2
1000 1000
2
10 10
20 20
3
1 1
2 2
1000 1000
Sample Output
1414.2
oh!
-------------------------------------------------------------
--------------------------------------------------------------
oh!
-------------------------------------------------------------
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int bin[105];
int n;//n个小岛
int counta;
double result;
struct point//小岛坐标
{
int x;
int y;
};
struct dis
{
int a;//第a小个岛
int b;//第b个小岛
double distance;//距离
}d[10005];//保存每个小岛间的距离
double liangdianjuli(point a,point b)
{
double jl=sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
return jl;
}
int cmp(dis a,dis b)// 距离从小到大排序
{
if(a.distance<b.distance)
{
return 1;
}
return 0;
}
//------------并查集--------------
int find(int x)
{
if(bin[x]!=x) bin[x]=find(bin[x]);
return bin[x];
}
void merge(dis d,double &p)
{
int a,b;
a=find(d.a);
b=find(d.b);
if(a!=b)
{
counta+=1;
bin[a]=b;
p+=d.distance;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
bin[i]=i;//并查集初始化
}
point p[105];//小岛
for(i=1;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
//-----------输出每个点的距离----------
int j;
int dweizhi=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
double juli;
juli=liangdianjuli(p[i],p[j]);
if(juli>=10&&juli<=1000)
{
d[dweizhi].a=i;
d[dweizhi].b=j;
d[dweizhi].distance=juli;
dweizhi+=1;
}
}
}
//----------开始排序-----------
sort(d,d+dweizhi,cmp);
//----------开始并查集 --------
result=0;
counta=1;
for(i=0;i<dweizhi;i++)
{
merge(d[i],result);
}
if(counta!=n)
printf("oh!\n");
else
printf("%.1lf\n",result*100);
}
return 0;
}
#include <stdio.h>
#include<iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int t,n,i,j,counts;
int Set[105],k;
double s;
struct Point
{
int x,y;
}point[105];
struct Distance
{
int a,b;
double l;
}dist[50000];
int find_root(int c)
{
if(Set[c]!=c)
Set[c] = find_root(Set[c]);
return Set[c];
}
void Union(int i)
{
int fx = find_root(dist[i].a);
int fy = find_root(dist[i].b);
if(dist[i].l>=10&&dist[i].l<=1000&&fx!=fy)
{
Set[fy] = fx;
s+=dist[i].l;
}
}
bool cmp(Distance d1,Distance d2)
{
return d1.l<d2.l;
}
int main()
{
scanf("%d",&t);
while(t--)
{
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
Set[i] = i;
for(i=1;i<=n;i++)
{
// Set[i] = i;
scanf("%d%d",&point[i].x,&point[i].y);
}
for(i=1 ,counts = 1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
dist[counts].a =i;
dist[counts].b =j;
double lx = (double)(point[i].x-point[j].x);
double ly = (double)(point[i].y-point[j].y);
dist[counts].l =sqrt(lx*lx+ly*ly);
counts++;
}
sort(dist+1,dist+1+counts,cmp);
for(i=1,s=0;i<=counts;i++) //刚才S在定义时初始化//刚才没写counts,而是n
Union(i);
k = 0;
for(i=1;i<=n;i++)
if(Set[i] == i)
k++;
if(k>2)
printf("oh!\n");
else
printf("%.1f\n",s*100);
}
}
return 0;
}