帮助设计一个程序,最短径路方面的(100)

  • 主题发起人 主题发起人 qsd101
  • 开始时间 开始时间
Q

qsd101

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个小的table,三个字段 from、to、licheng,比如从哈尔滨到沈阳、沈阳到北京、北京到天津,哈尔滨到沈阳2、沈阳2到北京,几个记录,怎么写代码能实现最短路径呢。谢谢,往详细告知!!!
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type Tlucheng = record FFrom: string; FTo: string; LiCheng: integer;//经过的路程 IsUse: boolean;//遍历标识 Citys: string;//经历过的城市 end;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; Edit2: TEdit; Button3: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); private ChinaInfo: array of Tlucheng; { Private declarations } function GetMaxCityLeng(s1, s2: string; len: integer; citys: string): string; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin Setlength(ChinaInfo, 10); ChinaInfo[0].FFrom := 'H'; ChinaInfo[0].FTo := 'B'; ChinaInfo[0].LICheng := 1200;//对应数据库的字段值 ChinaInfo[0].IsUse := false; //初始化为false ChinaInfo[0].Citys := '';//初始化为空 ChinaInfo[1].FFrom := 'H'; ChinaInfo[1].FTo := 'C'; ChinaInfo[1].LICheng := 200; ChinaInfo[1].IsUse := false; ChinaInfo[1].Citys := ''; ChinaInfo[2].FFrom := 'C'; ChinaInfo[2].FTo := 'B'; ChinaInfo[2].LICheng := 200; ChinaInfo[2].IsUse := false; ChinaInfo[2].Citys := ''; ChinaInfo[3].FFrom := 'B'; ChinaInfo[3].FTo := 'C'; ChinaInfo[3].LICheng := 200; ChinaInfo[3].IsUse := false; ChinaInfo[3].Citys := '';end;function TForm1.GetMaxCityLeng(s1, s2: string; len: integer; citys: string): string;var i: integer; MaxLen: integer; SCity: string;begin Result := ''; for i := 0 to length(ChinaInfo) - 1 do begin if not ChinaInfo.IsUse then begin if ChinaInfo.FFrom = s1 then begin ChinaInfo.IsUse := true; ChinaInfo.Citys := citys + '|' + s1; ChinaInfo.LICheng := len + ChinaInfo.LICheng; if ChinaInfo.FTo = s2 then begin ChinaInfo.Citys := ChinaInfo.Citys + '|' + s2; end else begin GetMaxCityLeng(ChinaInfo.FTo, s2, ChinaInfo.LICheng, ChinaInfo.Citys); end; end; end; end; MaxLen := 0; SCity := ''; for i := 0 to length(ChinaInfo) - 1 do begin if (ChinaInfo.IsUse) and (copy(ChinaInfo.Citys, 2, length(s1)) = s1) and (copy(ChinaInfo.Citys, length(ChinaInfo.Citys) - length(s2) + length('|'), length(s2)) = s2) then begin if ChinaInfo.LICheng > MaxLen then begin MaxLen := ChinaInfo.LICheng; SCity := ChinaInfo.Citys; end; end; end; Result := '经过的城市为:' + SCity + ' 路程为:' + inttostr(MaxLen);end;procedure TForm1.Button3Click(Sender: TObject);begin showmessage(GetMaxCityLeng('H', 'B', 0, ''));//调用end;end.//////////
 
这个是最长路径的,楼主自己改成最短路径的吧
 
function TForm1.GetMaxCityLeng(s1, s2: string; IsMaxLen: boolean): string; procedure SetCityInfo(s1, s2: string; len: integer; citys: string); var i: integer; begin for i := 0 to length(ChinaInfo) - 1 do begin if not ChinaInfo.IsUse then begin if ChinaInfo.FFrom = s1 then begin ChinaInfo.IsUse := true; ChinaInfo.Citys := citys + '|' + s1; ChinaInfo.LICheng := len + ChinaInfo.LICheng; if ChinaInfo.FTo = s2 then begin ChinaInfo.Citys := ChinaInfo.Citys + '|' + s2; end else begin SetCityInfo(ChinaInfo.FTo, s2, ChinaInfo.LICheng, ChinaInfo.Citys); end; end; end; end; end;var i: integer; MaxLen: integer; SCity: string;begin SetCityInfo(s1, s2, 0, ''); Result := ''; if IsMaxLen then MaxLen := 0 else MaxLen := 9999999; SCity := ''; for i := 0 to length(ChinaInfo) - 1 do begin if (ChinaInfo.IsUse) and (copy(ChinaInfo.Citys, 2, length(s1)) = s1) and (copy(ChinaInfo.Citys, length(ChinaInfo.Citys) - length(s2) + length('|'), length(s2)) = s2) then begin if (IsMaxLen and (ChinaInfo.LICheng > MaxLen)) or (not IsMaxLen and (ChinaInfo.LICheng < MaxLen)) then begin MaxLen := ChinaInfo.LICheng; SCity := ChinaInfo.Citys; end; end; end; Result := '经过的城市为:' + SCity + ' 路程为:' + inttostr(MaxLen);end;调用 showmessage(GetMaxCityLeng('H', 'B', true));//优化了下,参数为true是最长路径,false是最短路径
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
806
import
I
后退
顶部