日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最短路径问题的算法实现【转载】

發布時間:2024/4/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最短路径问题的算法实现【转载】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最短路徑問題的算法實現【轉載】

?文章來源:http://www.gissea.cn/html/2006-06/487.htm

?

?本例以由拓撲關系的arc/info 文件為數據源。其中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

?

?

相關算法文章:http://www.cnblogs.com/supersyg/articles/1011600.html

轉載于:https://www.cnblogs.com/lauer0246/archive/2008/08/29/1279811.html

總結

以上是生活随笔為你收集整理的最短路径问题的算法实现【转载】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。