剑鱼行动(普里姆算法)
题意
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(x[i]-x[j])+sqr(y[i]-y[j]))
var
n,i,j,k,t:longint;
tj,min:real;
a:array[0..200,0..200]of real;
x,y:array[0..200]of real;
f:array[0..200]of longint;
begin
readln(n);
fillchar(a,sizeof(a),0);
fillchar(f,sizeof(f),0);
for i:=1 to n do
readln(x[i],y[i]);
for i:=1 to n do
for j:=1 to n do
a[i,j]:=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
f[1]:=1;
tj:=0;
for i:=1 to n-1 do
begin
min:=maxlongint;
for j:=1 to n do
if f[j]=1 then
for k:=1 to n do
if f[k]=0 then
if (a[j,k]<min)and(a[j,k]<>0) then
begin
min:=a[j,k];
t:=k;
end;
if min<>maxint then
begin
tj:=tj+min;
f[t]:=1;
end;
end;
write(tj:0:2);
end.