Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const
- sz = 5000;
- var
- g:array[0..sz,0..sz] of longint;
- d:array[0..sz] of int64;
- used:array[0..sz] of boolean;
- i,j,n,m,u,min,s,e:longint;
- procedure input;
- var i,j:longint;
- begin
- read(n,s,e);
- for i:=1 to n do
- for j:=1 to n do
- begin
- read(g[i][j]);
- if g[i][j] = -1 then
- g[i][j]:=maxlongint shr 1;
- end;
- end;
- procedure init;
- var i:longint;
- begin
- for i:=1 to n do
- if i <> s then
- d[i]:=maxlongint;
- end;
- procedure Dijkstra;
- var i:longint;
- begin
- for i:=1 to n do
- begin
- min:=maxlongint;
- for j:=1 to n do
- if (min > d[j]) and (not used[j]) then
- begin
- min:=d[j];
- u:=j;
- end;
- used[u]:=true;
- for j:=1 to n do
- if (d[j] > d[u] + g[u][j]) and (not used[j]) then
- d[j]:= d[u] + g[u][j];
- end;
- end;
- procedure answer;
- begin
- if d[e] < maxlongint shr 1 then
- writeln(d[e])
- else
- writeln(-1);
- end;
- begin
- input;
- init;
- Dijkstra;
- answer;
- end.
Advertisement
Add Comment
Please, Sign In to add comment