Скачиваний:
26
Добавлен:
02.05.2014
Размер:
5.15 Кб
Скачать
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, Grids, Mask;

type
TForm1 = class(TForm)
SpinEdit1: TSpinEdit;
StringGrid1: TStringGrid;
Button1: TButton;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure Button1Click(Sender: TObject);
procedure StringGrid1Exit(Sender: TObject);
procedure StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
str:string;
x,n,izm,k,i,j,col,row:integer;
m: array [0..95,1..3] of integer;
otvet: array [1..14,1..3] of integer;
vershina: array [1..15] of integer;
implementation

{$R *.dfm}

procedure razmer;
begin
k:=form1.SpinEdit1.Value+1;
form1.StringGrid1.Width:=k*22-(form1.SpinEdit1.Value div 3)-(form1.SpinEdit1.Value div 5)-(form1.SpinEdit1.Value div 7)-(form1.SpinEdit1.Value div 8);
form1.StringGrid1.Height:=k*22-(form1.SpinEdit1.Value div 3)-(form1.SpinEdit1.Value div 5)-(form1.SpinEdit1.Value div 7)-(form1.SpinEdit1.Value div 8);
form1.StringGrid1.ColCount:=k;
form1.StringGrid1.RowCount:=k;
form1.ListBox1.Left:=25+form1.StringGrid1.Width;
form1.ListBox1.Height:=(3*form1.StringGrid1.Height) div 5;
form1.Width:=128+form1.StringGrid1.Width;
form1.Height:=83+form1.StringGrid1.Height;
form1.ListBox1.Items.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
razmer;
for i:=1 to k-1 do
begin
form1.StringGrid1.Cells[0,i]:=inttostr(i);
form1.StringGrid1.Cells[i,0]:=inttostr(i);
form1.StringGrid1.Cells[i,i]:='0';
end;
izm:=k;
end;

procedure prov_matrici;
begin
if (StrToIntDef(form1.StringGrid1.Cells[col,row],-9999)=-9999)or((StrToIntDef(form1.StringGrid1.Cells[col,row],-9999)=0)and(row<>col)) then
begin
form1.StringGrid1.Cells[col,row]:='';
form1.StringGrid1.Cells[row,col]:='';
end
else
begin
str:=inttostr(abs(strtoint(form1.StringGrid1.Cells[col,row])));
form1.StringGrid1.Cells[col,row]:=str;
form1.StringGrid1.Cells[row,col]:=str;
end;
form1.StringGrid1.Cells[0,0]:='';
for i:=1 to k-1 do
begin
form1.StringGrid1.Cells[i,0]:=inttostr(i);
form1.StringGrid1.Cells[0,i]:=inttostr(i);
if k>izm then
begin
form1.StringGrid1.Cells[i,izm]:='';
form1.StringGrid1.Cells[izm,i]:='';
end;
form1.StringGrid1.Cells[i,i]:='0';
end;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
razmer;
prov_matrici;
izm:=k;
end;

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
prov_matrici;
end;

procedure TForm1.StringGrid1Exit(Sender: TObject);
begin
prov_matrici;
end;

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin
col:=ACol;
row:=ARow;
end;

procedure proc_prima_krascala;
begin
for i:=1 to form1.SpinEdit1.Value do
vershina[i]:=i;
x:=0;
for i:=1 to n do
if vershina[m[i,1]]<>vershina[m[i,2]] then
begin
x:=x+1;
otvet[x,1]:=m[i,1];
otvet[x,2]:=m[i,2];
otvet[x,3]:=m[i,3];
k:=vershina[m[i,2]];
for j:=1 to form1.SpinEdit1.Value do
if vershina[j]=k then vershina[j]:=vershina[m[i,1]];
end;
k:=0;
form1.ListBox1.Items.Clear;
for i:=1 to x do
begin
form1.ListBox1.Items.Add(inttostr(otvet[i,1])+','+inttostr(otvet[i,2])+' = '+inttostr(otvet[i,3]));
k:=k+otvet[i,3];
end;
j:=0;
for i:=1 to form1.SpinEdit1.Value-1 do
if (vershina[i]<>vershina[i+1])or(vershina[i]=0) then j:=1;
if j=0 then
form1.ListBox1.Items.Add('Ответ: '+inttostr(k))
else form1.ListBox1.Items.Add('Граф не связный');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
x:=form1.SpinEdit1.Value;
n:=0;
for i:=1 to x do
for j:=i to x do
if StrToIntDef(form1.StringGrid1.Cells[j,i],-1)>0 then
begin
n:=n+1;
m[n,1]:=StrToInt(form1.StringGrid1.Cells[0,i]);
m[n,2]:=StrToInt(form1.StringGrid1.Cells[j,0]);
m[n,3]:=StrToIntDef(form1.StringGrid1.Cells[j,i],-1);
end;
for i:=1 to n-1 do
for j:= 1 to n-1 do
if m[j,3]>m[j+1,3] then
begin
m[0,1]:=m[j+1,1];
m[0,2]:=m[j+1,2];
m[0,3]:=m[j+1,3];
m[j+1,1]:=m[j,1];
m[j+1,2]:=m[j,2];
m[j+1,3]:=m[j,3];
m[j,1]:=m[0,1];
m[j,2]:=m[0,2];
m[j,3]:=m[0,3];
end;
proc_prima_krascala;
end;

procedure TForm1.StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
randomize;
form1.ListBox1.Items.Clear;
for i:=1 to form1.SpinEdit1.Value-1 do
for j:=i+1 to form1.SpinEdit1.Value do
begin
form1.StringGrid1.Cells[j,i]:=inttostr(random(99));
form1.StringGrid1.Cells[i,j]:=form1.StringGrid1.Cells[j,i];
end;
end;

end.
Соседние файлы в папке Алгоритм построения минимального остовного дерева
  • #
    02.05.20149.7 Кб27Unit1.dcu
  • #
    02.05.20141.59 Кб26Unit1.dfm
  • #
    02.05.20145.15 Кб26Unit1.pas