Dijkstra

 1 var i,j,n,s,t,p,min:longint;
2 a:array[1..1000,1..1000]of longint;
3 d:array[1..1000]of longint;
4 v:array[1..1000]of boolean;
5
6
7
8 procedure dijkstra(s:longint);
9 begin
10 fillchar(d,sizeof(d),$7f);
11 fillchar(v,sizeof(v),false);
12 d[s]:=0;
13 for j:=2 to n do begin
14 min:=maxlongint;
15 for i:=1 to n do
16 if (not v[i])and(d[i]<min) then begin
17 p:=i; min:=d[i];
18 end;
19 v[p]:=true;
20 for i:=1 to n do
21 if (not v[i])and(a[p,i]<>0)and
22 (d[p] a[p,i]<d[i]) then d[i]:=d[p] a[p,i];
23 end;
24 end;
25
26
27
28 begin
29 readln(n);
30 for i:=1 to n do
31 for j:=1 to n do read(a[i,j]);
32 readln(s,t);
33 dijkstra(s);
34 writeln(d[t]);
35 end.

posted on 2012-01-06 17:29  Lex Luthor  阅读(182)  评论(0编辑  收藏  举报

导航