标签:
var n,m,i,j,k,s,t,p,min:longint;
dis:array[0..100] of longint;
f:array[0..100] of boolean;
map:array[0..100,0..100] of longint;
procedure prim;
var i,j:longint;
begin
fillchar(f,sizeof(f),true);
f[1]:=false;
for i:=1 to n do dis[i]:=map[1,i];//记录第一个点的到其它点距离
for i:=1 to n-1 do
begin
//找出当前点到其它点的最小值,并记录新的点P
min:=maxlongint;
for j:=1 to n do
if f[j] and (min>dis[j]) then
begin
min:=dis[j];
p:=j;
end;
f[p]:=true;//新的最小值的点不再参与比较
s:=s+dis[p];//记录最短值
for j:=1 to n do //新点到其它点的距离与原距离比较并更新小的
if f[j] and (dis[j]>map[p,j]) then
dis[j]:=map[p,j];
end;
end;
begin
readln(n);
for i:=1 to n do
begin
for j:=1 to n do read(map[i,j]);
readln;
end;
prim;
writeln(s);
end.
最小生成树Prim——最短网络(洛谷1546)(USACO 3.1.1)可当Prim模板
标签:
原文地址:http://www.cnblogs.com/zqzxwdm/p/4743766.html