首页 > 代码库 > prim算法模板

prim算法模板

var g:array[1..10,1..10] of longint;  d:array[1..10] of longint;  f:array[1..10] of boolean;procedure prim;  var i,j,k,min:longint;  begin    fillchar(g,sizeof(g),0);    fillchar(f,sizeof(f),0);    for i:=1 to n do d[i]:=g[1,i];    f[1]:=true;    for i:=2 to n do      begin        min:=oo;        for j:=1 to n do          if (f[j]=false) and (d[j]<min) then            begin{这个边必须跨越两个集合}              min:=d[j];              k:=j;            end;              f[k]:=true;              for j:=1 to n do//修改距离                if (not f[j]) and (g[k,j]<d[j])) then                  d[j]:=g[k,j];            end;  end;