T1:
先把每两个点之间建立边,然后将边小于C的砍掉,再用最小生成树算法
参考程序:
var n,m,i,j,x,y,bz,lenb,ans,t1,t2:longint;
a:array[1..2000,1..2]of longint;
f:array[1..2000,1..2000]of longint;
b:array[0..4000000,1..3]of longint;
c:array[1..2000]of longint;
function father(x:longint):longint;
begin
if c[x]=x then exit(x) else exit(father(c[x]));
end;
procedure kuaipai(l,r:longint);
var i,j,mid:longint;
begin
i:=l;j:=r;
mid:=b[(l+r)div 2,3];
repeat
while b[i,3]<mid do inc(i);
while b[j,3]>mid do dec(j);
if i<=j then
begin
b[0]:=b[i];
b[i]:=b[j];
b[j]:=b[0];
inc(i);
dec(j);
end;
until i>j;
if l<j then kuaipai(l,j);
if i<r then kuaipai(i,r)
end;
begin
readln(n,m);
for i:=1 to n do
begin
readln(x,y);
a[i,1]:=x;
a[i,2]:=y;
end;
for i:=1 to n do
begin
bz:=0;
for j:=1 to n do
if (a[i,1]-a[j,1])*(a[i,1]-a[j,1])+(a[i,2]-a[j,2])*(a[i,2]-a[j,2])>=m then
begin
f[i,j]:=(a[i,1]-a[j,1])*(a[i,1]-a[j,1])+(a[i,2]-a[j,2])*(a[i,2]-a[j,2]);
inc(lenb);
b[lenb,1]:=i;
b[lenb,2]:=j;
b[lenb,3]:=f[i,j];
bz:=1;
end;
if bz=0 then
begin
writeln(-1);
exit;
end;
end;
kuaipai(1,lenb);
for i:=1 to lenb do c[b[i,1]]:=b[i,1];
for i:=1 to lenb do
begin
c[b[i,1]]:=father(b[i,1]);
c[b[i,2]]:=father(b[i,2]);
if c[b[i,1]]<>c[b[i,2]] then
begin
c[c[b[i,2]]]:=c[b[i,1]];
ans:=ans+b[i,3];
end;
end;
writeln(ans);
end.