剑鱼行动
Time Limit:10000MS Memory Limit:65536K
Total Submit:108 Accepted:88
Case Time Limit:1000MS
Description
给出N个点的坐标,对它们建立一个最小生成树,代价就是连接它们的路径的长度,现要求总长度最小。N的值在100以内,坐标值在[-10000,10000].结果保留二位小数
Input
5 —————5个点
0 0 —————5个点点的坐标
0 1
1 1
1 0
0.5 0.5
Output
2.83
求将两点的距离公式如下:sqrt(sqr(x1-x2)+sqr(y1-y2))
var
n,i,j,k,t:longint;
min,ans:real;
v:array[0..101]of boolean;
x,y:array[0..101]of real;
a:array[0..101,0..101]of real;
begin
readln(n);
for i:=1 to n do
begin
readln(x[i],y[i]);
for j:=1 to i-1 do
begin
a[i,j]:=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
a[j,i]:=a[i,j];
end;
end;
v[1]:=true;
for i:=1 to n-1 do
begin
min:=maxlongint;
for j:=1 to n do
if v[j] then
for k:=1 to n do
if v[k]=false then
if (a[j,k]<min)and(a[j,k]<>0) then
begin
min:=a[j,k];
t:=k;
end;
if min<>maxlongint then
begin
ans:=ans+min;
v[t]:=true;
end;
end;
write(ans:0:2);
end.