剑鱼行动
Time Limit:10000MS Memory Limit:65536K
Total Submit:98 Accepted:83
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
-
var a:array[-101..101,-101..101] of extended; v:array[1..101] of longint; x,y:array[1..101] of extended; n,m,j,k,l,i,q,p:longint; min,ans:extended; begin readln(n); for i:=1 to n do begin read(x[i],y[i]); v[i]:=i; end; for i:=1 to n do for j:=1 to n do if i<>j then a[i,j]:=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); for k:=1 to n-1 do begin min:=maxlongint; for i:=1 to n do for j:=1 to n do if (v[i]<>v[j]) and (a[i,j]<>0) and (a[i,j]<min) then begin min:=a[i,j]; p:=j; q:=i; end; ans:=ans+min; l:=v[p]; for i:=1 to n do if v[i]=l then v[i]:=v[q]; end; writeln(ans:0:2); end.