Here is a translation. It compiles but I've not run it, so bugs may exist:
[pascal]program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
MAX_LINKS = 100;
INFINITY = 999999;
EXCLUDED = 999998;
type
TNode = class
private
FName : string;
protected
public
Weight: array[0..MAX_LINKS - 1] of Integer;
Linked : array[0..MAX_LINKS - 1] of TNode;
Visited: array[0..MAX_LINKS - 1] of Boolean;
constructor Create;
property Name: string read FName write FName;
end;
//------------------------------------------------------------------------------
constructor TNode.Create;
var
i: Integer;
begin
inherited;
for i := 0 to MAX_LINKS - 1 do
begin
Linked[i] := nil;
Visited[i] := False;
Weight[i] := 0;
end;
end;
//------------------------------------------------------------------------------
var
Linker : Integer = 0;
Length : Integer = 0;
Lengths: array[0..MAX_LINKS - 1] of Integer;
Final_Lengths: array[0..MAX_LINKS - 1] of Integer;
Parents: array[0..MAX_LINKS - 1] of Integer;
GPointer: Integer = 0;
Start : Integer = 1;
Finish : Integer = 0;
Graph : array[0..MAX_LINKS - 1] of TNode;
//------------------------------------------------------------------------------
procedure Pause(const Msg: string);
begin
WriteLn(#13#10, Msg);
Read;
end;
//------------------------------------------------------------------------------
procedure Draw_Graph;
var
x: Integer;
y: Integer;
begin
WriteLn(#13#10, '************* Drawing graph ********************');
for x := 1 to GPointer do
begin
for y := 1 to MAX_LINKS - 1 do
begin
if Graph[x].Linked[y] <> nil then
begin
WriteLn(#13#10 + Graph[x].Name, ' -> ', Graph[x].Linked[y].Name, ' :: weight=', Graph[x].Weight[y]);
end;
end;
end;
WriteLn(#13#10, '************************************************' , #13#10);
end;
//------------------------------------------------------------------------------
function sysedi(From, Target: TNode): Boolean;
var
i: Integer;
begin
for i := 0 to MAX_LINKS - 1 do
begin
if (From.Linked[i] <> nil) and (From.Linked[i].Name = Target.Name) then
begin
Linker := i;
Result := True;
Exit;
end;
end;
Result := False;
end;
//------------------------------------------------------------------------------
function Find_Smallest(masiv: array of Integer): Integer;
var
Min: Integer;
i : Integer;
begin
Result := 0;
Min := INFINITY;
for i := 1 to GPointer do
begin
if Masiv[i] < Min then
begin
Min := Masiv[i];
Result := i;
end;
end;
end;
//------------------------------------------------------------------------------
procedure Update_Lengths(vryh: Integer);
var
i: Integer;
begin
for i := 1 to GPointer do
begin
if (Lengths[i] > Lengths[vryh]) and (Graph[i] <> Graph[vryh]) then
begin
if (Lengths[i] <> EXCLUDED) and (sysedi(graph[vryh], graph[i])) and (graph[vryh].weight[Linker] + Lengths[vryh] < Lengths[i]) then
begin
Lengths[i] := (Graph[vryh].Weight[Linker]) + Lengths[vryh];
Parents[i] := vryh;
end;
end;
end;
Final_Lengths[vryh] := Lengths[vryh];
Lengths[vryh] := EXCLUDED;
end;
//------------------------------------------------------------------------------
function ima_non_infinit: Boolean;
var
i: Integer;
begin
for i := 1 to GPointer do
begin
if lengths[i] < EXCLUDED then
begin
Result := True;
Exit;
end;
end;
Result := False;
end;
//------------------------------------------------------------------------------
procedure ShowPath(To_: Integer);
begin
if Lengths[To_] = INFINITY then
begin
WriteLn(#13#10, 'No path!');
Exit;
end;
Write(Graph[To_].Name, ' <- ');
if Parents[Start] = Parents[to_] then
begin
Write(Graph[Start].Name);
Exit;
end;
ShowPath(Parents[To_]);
end;
//------------------------------------------------------------------------------
procedure Perform;
var
k: Integer;
begin
for k := 1 to GPointer do
begin
if Graph[k] = Graph[Start] then
begin
Lengths[K] := EXCLUDED;
end
else
if sysedi(Graph[Start], Graph[k]) then
begin
Lengths[K] := Graph[Start].Weight[Linker];
end
else
begin
Lengths[k] := INFINITY;
end;
Parents[k] := Start;
end;
while ima_non_infinit do
begin
Update_Lengths(Find_Smallest(Lengths));
end;
Update_Lengths(Find_Smallest(Lengths));
end;
//------------------------------------------------------------------------------
function Seek_Path: Boolean;
begin
WriteLn(#13#10, '************** Seeking path ********************');
WriteLn('Where we start from (1..', gpointer, ') [0=Exit]: ');
Read(Start);
if Start <> 0 then
begin
Perform;
Write('Seek path to (1..', GPointer, ') : ');
Read(Finish);
ShowPath(Finish);
WriteLn(#13#10, 'Length: ');
if Lengths[Finish] = INFINITY then
Write('INFINITY')
else
if Final_Lengths[Finish] = EXCLUDED then
Write('0')
else
begin
WriteLn(Final_Lengths[Finish]);
WriteLn('***************************************** *******');
end;
Result := True;
end
else
begin
WriteLn(#13#10, '************************************************' );
Result := False;
end;
end;
//------------------------------------------------------------------------------
procedure Main;
var
i: Integer;
Number: Integer; //broi vyzli v grafa
X: Integer; //rabotni broq4i
Y: Integer;
Weight: Integer;
begin
Number := 1;
//Weight := 0;
y := 0;
for i := Low(Graph) to High(Graph) do
Graph[i] := nil;
Write('Number of nodes in the graph: ');
Read(GPointer);
for x := 1 to GPointer do
begin
Graph[x] := TNode.Create;
Graph[x].Name := IntToStr(x);
end;
for x := 1 to GPointer do
begin
WriteLn;
while Number <> 0 do
begin
Write(' node ', x, ' is linked with (''0'' exits): ');
Read(Number);
if Number = 0 then
Break;
Graph[X].Linked[Y] := Graph[Number];
Write(' Weight between ', X, ' and ', number, ' = ');
Read(Weight);
WriteLn;
Graph[x].Weight[Y] := Weight;
Inc(Y);
end;
Number := 1;
Y := 0;
//Weight := 0;
end;
Draw_Graph;
while Seek_Path do
;
for i := Low(Graph) to High(Graph) do
Graph[i].Free;
ReadLn;
end;
//------------------------------------------------------------------------------
begin
Main;
end.[/pascal]
Bookmarks