用文件输入输出
input: n,k
输入一个矩阵表示边的信息
output: n个数,表示k到各个点的最短路
program dijkstra;
const
inp = 'input.txt';
oup = 'output.txt';
maxn= 100;
var
ga : array[1..maxn,1..maxn] of integer;
dist : array[1..maxn] of integer;
s : array[1..maxn] of 0..1;
n,k : integer;
fp : text;
procedure init;
var
i,j: integer;
begin
assign(fp,inp); reset(fp);
readln(fp,n,k);
for i:=1 to n do
for j:=1 to n do
read(fp,ga[i,j]);
close(fp);
end;
procedure main;
var
i,j,w,m:integer;
begin
fillchar(s,sizeof(s),0);
for i:=1 to n do
dist[i]:=maxint;
dist[k]:=0;
for i:=1 to n-1 do
begin
m:=maxint;
for j:=1 to n do
if (s[j]=0) and (dist[j]<m) then
begin
m:=dist[j];
w:=j;
end;
s[w]:=1;
for j:=1 to n do
if (s[j]=0) and (ga[w,j]>0) and (dist[w]+ga[w,j]<dist[j]) then
dist[j]:=dist[w]+ga[w,j];
end;
end;
procedure print;
var
i,j:integer;
begin
assign(fp,oup);
rewrite(fp);
for i:=1 to n do
write(fp,dist[i],' ');
close(fp);
end;
begin
init;
main;
print;
end.
posted on 2005-08-17 09:31
李青 阅读(1904)
评论(0) 编辑 收藏 引用