最小生成树prime+heep

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值