您的位置:首页 > 其它

Hdu 1162 Eddy's picture【最小生成树】

2015-08-14 00:48 543 查看

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8141    Accepted Submission(s): 4130

[align=left]Problem Description[/align]
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it
can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.

Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length
which the ink draws?

 

[align=left]Input[/align]
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.

 

[align=left]Output[/align]
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.

 

[align=left]Sample Input[/align]

3
1.0 1.0
2.0 2.0
2.0 4.0

 

[align=left]Sample Output[/align]

3.41

一道简单的最小生成树题目,只需要求出最小生成树的总权值就可以了,水过.....

注意坐标和长度的转换...

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int per[205],n;
double a[205],b[205];
struct lu
{
int a,b;
double len;
}x[250005];
double dis(int i,int j)
{
return sqrt((a[i]-a[j])*(a[i]-a[j])*1.0+(b[i]-b[j])*(b[i]-b[j]));
}
void init()
{
for(int i=1;i<=n;++i)
{
per[i]=i;
}
}
bool cmp(lu a,lu b)
{
return a.len<b.len;
}
int find(int x)
{
int r=x;
while(r!=per[r])
{
r=per[r];
}
int i=x,j;
while(i!=r)
{
j=per[i];per[i]=r;i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
per[fy]=fx;
return 1;
}
return 0;
}
void kruskal()
{
int cnt=0;double maxn=0;
for(int i=0;cnt<n-1;++i)
{
if(join(x[i].a,x[i].b))
{
maxn+=x[i].len;
++cnt;
}
}
printf("%.2lf\n",maxn);
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
init();
for(i=0;i<n;++i)
{
scanf("%lf%lf",a+i,b+i);
}
int c=0;
for(i=0;i<n-1;++i)
{
for(j=i+1;j<n;++j)
{
x[c].a=i+1;x[c].b=j+1;
x[c].len=dis(i,j);
++c;
}
}
sort(x,x+c,cmp);
kruskal();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: