procedure TForm1.Button1Click(Sender: TObject);
const max=10000;
var
gLJJZ:array of array of Integer;//图的邻接矩阵,权值默认都是1
sList,sFGList:TStringList;
i,j,n,iR:Integer;
sRowNum,sNodes:string;
first,tail,u,min,y,m:Integer;
dist,path,p:array of Integer;
s:set of Byte;
begin
edit1.Text:='1';
edit2.Text:='13';
edit3.Text:='';
sList:=TStringList.Create;
try
sList.LoadFromFile('.\JD.txt');
if sList.Count<=0 then exit;
n:=StrToInt(sList[0]);//结点数目
SetLength(gLJJZ,n+1,n+1);
for i:=0 to n do
for j:=0 to n do
gLJJZ[i,j]:=max;
sFGList:=TStringList.Create;
try
for i:=1 to sList.Count-1 do
begin
sRowNum:=sList.Names[i];
iR:=StrToInt(sRowNum);
sNodes:=sList.Values[sRowNum];
sFGList.Delimiter:=',';
sFGList.DelimitedText:=sNodes;
for j:=0 to sFGList.Count-1 do
gLJJZ[iR,StrToInt(sFGList[j])]:=1;//权值都是1
end;
finally
sFGList.Free;
end;
finally
sList.Free;
end;
u:=first;
s:=[first];
while u<>tail do
begin
for j:=1 to n do
if not (j in s) and (dist[u]+gLJJZ[u,j]<dist[j]) then
begin
dist[j]:=dist[u]+gLJJZ[u,j];
path[j]:=u;
end;
min:=max;
for j:=1 to n do
if not (j in s) and (dist[j]<min) then
begin
u:=j;
min:=dist[j];
end;
if min=max then
begin
showmessage('No Answer');
exit;
end;
s:=s+[u];
end;
y:=tail;
m:=0;
while y<>first do
begin
inc(m);
p[m]:=y;
y:=path[y];
end;
sNodes:=inttostr(first)+' ';
for j:=m downto 1 do
sNodes:=sNodes+' '+IntToStr(p[j]);