dijkstra算法(Pascal描述)


const
dim=6;
max=200;

var
cost:array[1..dim,1..dim] of integer;
i,j:integer;

isfound:array[1..dim] of boolean;
distance:array[1..dim] of integer;
v0:integer;
vtemp:integer;
min:integer;
counter:integer;
c:integer;

begin
writeln('*** result ***');
assign(input,'in.txt');
reset(input);
for i:=1 to 6 do
begin
	for j:=1 to 6 do
	begin
		read(input,cost[i,j]);
		write(cost[i,j]:5);
	end;
	writeln;
end;

{init}
v0:=1;
for i:=1 to dim do
begin
	distance[i]:=cost[v0,i];
	isfound[i]:=false;
end;

distance[v0]:=0;
isfound[v0]:=true;

{search}

for counter:=1 to dim do
begin
	min:=max;
	for i:=1 to dim do
	begin
		if (distance[i]<min) and (not isfound[i]) then
		{if there is a path though i,j}
		begin
			min:=distance[i];
			vtemp:=i;
		end;
	end;
	
	writeln;
	isfound[vtemp]:=true;
	
	{update}
	for i:=1 to dim do
	begin
		if (min+cost[vtemp,i]<distance[i]) and (not isfound[i]) then
		begin
			distance[i]:=min+cost[vtemp,i];
			for c:=1 to dim do
			begin
				write(distance[c]:5);
			end;
			writeln;
		end;
	end;
end;

for i:=1 to dim do
begin
	write(isfound[i]:5);
end;
writeln;

for i:=1 to dim do
begin

	write(distance[i]:5);
end;
writeln;
readln;
end.

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/sjtu081200/article/details/47908039
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞