Тема: Алгоритм Флойда Задание: - написать программу нахождения минимальных весов вершин для заданного графа; - предусмотреть ввод массива с клавиатуры; - подготовить отчет содержащий исходный текст программы. Выполнение работы: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Buttons, Grids, StdCtrls; type TForm1 = class(TForm) sg: TStringGrid; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; SpeedButton3: TSpeedButton; SpeedButton4: TSpeedButton; sg2: TStringGrid; sg3: TStringGrid; Label1: TLabel; procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); procedure SpeedButton3Click(Sender: TObject); procedure SpeedButton4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; kol: word = 6; mas_d: array of array of integer; mas_s: array of array of byte; implementation {$R *.DFM} procedure TForm1.SpeedButton1Click(Sender: TObject); begin if kol > 9 then exit; inc(kol); Label1.Caption:=(IntToStr(kol)+' x '+IntToStr(kol)); sg.ColCount:=kol; sg.RowCount:=kol; sg.Width:=sg.Width+32; sg.Height:=sg.Height+26; sg2.ColCount:=kol+1; sg2.RowCount:=kol+1; sg3.ColCount:=kol+1; sg3.RowCount:=kol+1; end; procedure TForm1.SpeedButton2Click(Sender: TObject); begin if kol < 5 then exit; dec(kol); Label1.Caption:=(IntToStr(kol)+' x '+IntToStr(kol)); sg.ColCount:=kol; sg.RowCount:=kol; sg.Width:=sg.Width-31; sg.Height:=sg.Height-25; sg2.ColCount:=kol+1; sg2.RowCount:=kol+1; sg3.ColCount:=kol+1; sg3.RowCount:=kol+1; end; procedure TForm1.SpeedButton3Click(Sender: TObject); var i,j : integer; begin SetLength(mas_d,kol); SetLength(mas_s,kol); for i:=0 to kol-1 do begin SetLength(mas_d[i],kol); SetLength(mas_s[i],kol); end; for i:=0 to kol-1 do for j:=0 to kol-1 do begin if sg.Cells[j,i]='' then sg.Cells[j,i]:='9999'; mas_d[i][j]:=strtoint(sg.Cells[j,i]); if i=j then begin sg.Cells[i,j]:=' -'; mas_d[i][j]:=-1; end; mas_s[i][j]:=i+1; end; randomize; end; procedure TForm1.SpeedButton4Click(Sender: TObject); var i,j,k:integer; begin for k:=0 to kol-1 do for i:=0 to kol-1 do for j:=0 to kol-1 do begin if (mas_d[k][i] < 9999) and (mas_d[k][i] > -1) and (mas_d[k][j] < 9999) and (mas_d[k][j] > -1) and (i<>j) and (mas_d[k][i] + mas_d[k][j] < mas_d[j][i]) then begin mas_d[j][i]:= mas_d[k][i]+ mas_d[k][j]; mas_s[j][i]:=k+1; end; end; sg2.Visible:=true; sg3.Visible:=true; for i:=0 to kol+1 do begin sg2.Cells[i,0]:=IntToStr(i); sg2.Cells[0,i]:=IntToStr(i); sg3.Cells[i,0]:=IntToStr(i); sg3.Cells[0,i]:=IntToStr(i); end; for i:=0 to kol-1 do for j:=0 to kol-1 do begin sg2.Cells[j+1,i+1]:=IntToStr(mas_s[j,i]); sg3.Cells[j+1,i+1]:=IntToStr(mas_d[i,j]); if i=j then begin sg2.Cells[j+1,i+1]:=' -'; sg3.Cells[j+1,i+1]:=' -'; end; end; SpeedButton1.Enabled:=false; SpeedButton2.Enabled:=false; SpeedButton3.Enabled:=false; SpeedButton4.Enabled:=false; sg2.Width:=sg.Width+32; sg2.Height:=sg.Height+24; sg3.Width:=sg.Width+32; sg3.Height:=sg.Height+24; end; end.
|