當前位置:編程學習大全網 - 編程語言 - 用Mapbasic編程實現最短路徑查詢,誰有源程序給個,感激不盡!在線等

用Mapbasic編程實現最短路徑查詢,誰有源程序給個,感激不盡!在線等

這個不是我編寫的,網上八的。。。。

a1,b1,c1是以fnode排序生成的數組,a1對應fnode,b1對應tnode,c1對應length,同樣a2,b2,c2,是以tnode 生成的數組。Indexa1是對應某壹起點與其相連的終點的個數,indexb1時對應某壹終點與其相連的起點的個數,即其拓撲關系。

Public Function shortpath(startno As Integer, endno As Integer) As Single

以開始點,結束點為參數。

Dim result() As Single

Dim result1 As Integer

定義結果點

Dim s1 As Single

Dim min As Single

Dim ii, i, j, aa As Integer

Dim yc() As Boolean

Dim ycd() As Boolean

Dim rs1() As Single

Dim no() As Integer

Dim nopoint As Integer

ReDim yc(1 To maxno) As Boolean

ReDim ycd(1 To maxno) As Boolean

ReDim rs1(1 To maxno) As Single

ReDim result(1 To 2, 1 To maxno) As Single

定義結果,其中result(1,maxno)為結果點,result(2,maxno)為結果長度。

For i = 1 To maxno// maxno為網中最大的節點數。

yc(i) = False //標記已經查過的點。

ycd(i) = False //標記已經作結果點用過的點

rs1(i) = 1E+38 //假設從起點到任壹點的距離都為無窮大

Next i

ll = startno //設置開始點。

yc(ll) = True //標記開始點為真。即已經作結果點用過。

j = 0

For aa = 1 To maxno

先從與開始點相連的終點尋找

For i = 1 To indexa1(2, ll) //以與ll點相連的起點的個數循環

result1 = b1(indexa1(1, ll) - i + 1)找出與LL點相連的終點的點號

s1 = c1(indexa1(1, ll) - i + 1) + result(2, ll)找出長度並求和

If yc(result1) = True Then GoTo 200如果以被經查過進行下壹個

If ycd(result1) = True Then//如果已經作為結果點判斷哪壹個長

If rs1(result1) >= s1 Then//如果這壹點到起點的長度比現在的路線長,替代

rs1(result1) = s1

result(1, result1) = ll//設置到這點的最短路徑的前壹點為LL點(精華部分)

result(2, result1) = s1設置到這點的最短路徑長度

GoTo 200

Else

GoTo 200

End If

End If

如果上面的條件都不符合則進行下面的語句

ycd(result1) = True

rs1(result1) = s1

result(1, result1) = ll

result(2, result1) = s1

每找到壹個點加壹,為了下面的判斷

j = j + 1

ReDim Preserve no(1 To j) As Integer

從新 定義數組並使其值為當前的點號

no(j) = result1

200 Next I

再從與開始點相連的終點尋找,與上面壹樣不再標註

For i = 1 To indexb2(2, ll)

result1 = a2(indexb2(1, ll) - i + 1)

s1 = c2(indexb2(1, ll) - i + 1) + result(2, ll)

If yc(result1) = True Then GoTo 300

If ycd(result1) = True Then

If rs1(result1) >= s1 Then

rs1(result1) = s1

result(1, result1) = ll

result(2, result1) = s1

GoTo 300

Else

GoTo 300

End If

End If

ycd(result1) = True

rs1(result1) = s1

result(1, result1) = ll

result(2, result1) = s1

j = j + 1

ReDim Preserve no(1 To j) As Integer

no(j) = result1

300 Next I

設置最小為無窮大,最短路徑點為空

min = 1E+38

minpoint = Null

(優化部分)

找出已經查過點中長度最短的點

For i = aa To j

If min > rs1(no(i)) Then

ii = i

min = rs1(no(i))

minpoint = no(i)

End If

Next I

如果沒有結果,即起點與終點沒有通路退出程序

If min = 1E+38 Then Exit Function

(重點優化)將兩點互換,減少循環。

no(ii) = no(aa)

no(aa) = minpoint

標記已經作為結果點判斷過

yc(minpoint) = True

ll = minpoint

判斷結果點是否等於終點,如果等於則已經找到最短路徑

If minpoint = endno Then Exit For

Next aa

返回最短路徑長度

Stpath = result(2, endno)

End Function

  • 上一篇:氪編程軟件
  • 下一篇:跪求JASS教程
  • copyright 2024編程學習大全網