program as;
type
t1 =record
wz,shu :longint;
end;
var
n,heapsize,t :longint;
sum,totel :longint;
a :array[1..701,0..701] of longint;
bool :array[1..490001] of boolean;
heap :array[1..490001] of t1;
procedure duru;
var
i,j:longint;
begin
read(n); t:=1;
totel:=0;
heapsize:=0;
fillchar(bool,sizeof(bool),false);
bool[1]:=true;
for i:=1 to n do
for j:=1 to n do
read(a[i,j]);
end;
procedure insert(x,x1:longint);
var
l:longint;
begin
inc(heapsize);
l:=heapsize;
while (l<>1)and(x<heap[l div 2].shu) do
begin
heap[l]:=heap[l div 2];
l :=l div 2;
end;
heap[l].shu:=x;
heap[l].wz :=x1;
end;
procedure delete(i:longint);
var
cl:longint;
x :t1;
begin
x:=heap[heapsize];
dec(heapsize);
cl:=2*i;
while cl<=heapsize do
begin
if (cl<heapsize) and (heap[cl].shu>heap[cl+1].shu) then
inc(cl);
if x.shu<heap[cl].shu then break;
heap[i]:=heap[cl];
i:=cl;
cl:=i*2;
end;
heap[i]:=x;
end;
procedure im;
var
q :boolean;
jl,i1,i,j :longint;
begin
for i1:=1 to n do
begin
i:=t;
q:=false;
for j:=1 to n do
if (a[i,j]<>0)and(not bool[j])and(i<>j) then
begin
q:=true;
insert(a[i,j],j);
end;
if q then
begin
inc(totel);
bool[heap[1].wz]:=true;
sum :=sum+heap[1].shu;
if totel=n-1 then exit;
t:=heap[1].wz;
while bool[heap[1].wz] do
delete(1);
end;
end;
end;
begin
assign(input,'t1.in1');
reset(input);
assign(output,'t1.out1');
rewrite(output);
duru;
im;
writeln(sum);
close(input);
close(output);
end.
最小生成树prime+heep
最新推荐文章于 2023-06-07 10:00:00 发布