Problem Description
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?
Input
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.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
分析
最小生成树的模板
依然kruskal+并查集优化
刷了很多次都不对,改了输入预处理就过了
代码
type
edge=record
x,y:longint;
w:real;
end;
point=record
x,y:real;
end;
var
a,b,n,i:longint;
f:array[1..200]of longint;
e:array[1..20000]of edge;
p:array[1..200]of point;
maxE:longint;
ans:real;
function cal(x,y:point):real;
begin
exit(sqrt(sqr(x.x-y.x)+sqr(x.y-y.y)));
end;
procedure qsort(l,r:longint);
var
x,y:longint;
temp:edge;
key:real;
begin
if l>=r then exit;
x:=l;
y:=r;
key:=e[l+random(r-l+1)].w;
repeat
while (e[x].w<key) do inc(x);
while (e[y].w>key) do dec(y);
if x<=y then
begin
temp:=e[x];
e[x]:=e[y];
e[y]:=temp;
inc(x);
dec(y);
end;
until x>y;
qsort(l,y);
qsort(x,r);
end;
function find(x:longint):longint;
begin
if x<>f[x] then
f[x]:=find(f[x]);
exit(f[x]);
end;
procedure union(x,y:longint);
begin
x:=find(x);
y:=find(y);
if x<>y then
f[x]:=y;
end;
procedure init;
var
i,j:Longint;
begin
fillchar(p,sizeof(p),0);
fillchar(e,sizeof(e),0);
readln(n);
for i:=1 to n do
readln(p[i].x,p[i].y);
maxE:=0;
for i:=1 to n-1 do
for j:=i+1 to n do
begin
inc(maxE);
e[maxE].x:=i;
e[maxE].y:=j;
e[maxE].w:=cal(p[i],p[j]);
end;
end;
begin
randomize;
while not seekeof do
begin
init;
qsort(1,maxE);
ans:=0;
for i:=1 to n do f[i]:=i;
for i:=1 to maxE do
if find(e[i].x)<>find(e[i].y) then
begin
union(e[i].x,e[i].y);
ans:=e[i].w+ans;
end;
writeln(ans:0:2);
end;
end.