pascal普里姆算法

来源:百度知道 编辑:UC知道 时间:2024/09/27 07:19:28
type edge=record
fr,en,we:integer;
end;
var g:array[1..100,1..100] of integer;
ed:array[1..100] of edge;
n,i,j,k,min,m,s,w:integer;
t:edge;
begin
assign(input,'creat.in');
assign(output,'creat.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
begin
for j:=1 to n do
begin
read(g[i,j]);
if g[i,j]=0 then
g[i,j]:=maxint;
end;
readln;
end;
for i:=1 to n-1 do
begin
ed[i].fr:=1;
ed[i].en:=i+1;
ed[i].we:=g[i,i+1];
if ed[i].we=0 then
ed[i].we:=maxint;
end;
for k:=1 to n-1 do
begin
min:=maxint;
m:=k;
for j:=k to n-1 do
if (ed[j].we<min)and(ed[j].we<>0) then
begin
MIN:=ED[J].WE;
M:=J;
END;

program prim; {理工P106 输出:各个顶点的指向 注意初始的距阵,无连接的边表示为0 后被赋值maxint}
const maxn=100; {无最小生成树的权值和无生成时取点顺序记录,只有指向}
var cost:array[1..maxn,1..maxn] of integer;
mincost,closed:array[1..maxn]of integer;
min,k,i,j,n,x,y:integer;
begin
assign( input,'prim1.in'); reset(input);
assign(output,'prim1.out');rewrite(output);
readln(n);
for i:=1 to n do begin
for j:=1 to n do begin
read(cost[i,j]);
if (i<>j) and (cost[i,j]=0) then cost[i,j]:=maxint;
end;
readln;
end;
for i:=1 to n do begin
mincost[i]:=cost[1,i]; closed[i]:=1;end;
for i:=2 to n do begin
min:=maxint;
for j:=1 to n do
if (mincost[j]<min) and (mincost[j]<>0) then begin
min:=mincost[j];