不好意思,小弟正在用vb开发,相信vb对于各位vc高手是小case.对于同一个邻接矩阵,分别用两个方法求解,竟然出现个别不同的路径,由得到的路径长度来比较,是floyd的短一点,是不是我的程序写错了,大家给看一下。Option Explicit
Dim Cost(), DotPos() As Double
Dim DotMaxNo, LineMaxNo As Integer
Const MAX = 1E+38
Dim B_query As Boolean
Dim a(), path() As Double
'DotMaxNo 是最大接点数
'其中cost()中保存有要求解的图的邻接矩阵。
floyd:
ReDim a(DotMaxNo, DotMaxNo), path(DotMaxNo, DotMaxNo)Dim n As Integer
Dim i, j, k, f, t As Integer
n = DotMaxNoFor i = 1 To n
For j = 1 To n
a(i, j) = Cost(i, j)
path(i, j) = 0
Next j
Next iFor k = 1 To n
For i = 1 To n
For j = 1 To n
If a(i, k) + a(k, j) < a(i, j) Then
a(i, j) = a(i, k) + a(k, j)
path(i, j) = k
End If
Next
Next
NextGoTo 300End IfText1.Text = ""
Dim c, d, e As Integer v = Val(Text2.Text)'开始接点
t = Val(Text3.Text)'终止点Text1.Text = Text1.Text & a(f, t) & "=="200:
e = path(f, t)If e = 0 Then
GoTo 300
End If
Text1.Text = Text1.Text & "-->" & e
t = e
GoTo 200
f = e
GoTo 200
300:
’*******************************
dijkstra:
Dim dist(), pre(), s() As Double ReDim dist(DotMaxNo), pre(DotMaxNo), s(DotMaxNo)
Dim i, j, k, min, n, v, t, tt, e As Integer v = Val(Text2.Text)'开始接点
t = Val(Text3.Text)'终止点
tt = t
n = DotMaxNo For i = 1 To n
dist(i) = Cost(v, i)
s(i) = 0
If dist(i) < MAX Then
pre(i) = v
Else
pre(i) = 0
End If
Next s(v) = 1
pre(v) = 0
For i = 1 To n
min = MAX
k = 0
For j = 1 To n
If s(j) = 0 Then
If dist(j) <> 0 And dist(j) < min Then
min = dist(j)
k = j
End If
End If
Next j
If k = 0 Then
GoTo 100
End If
s(k) = 1
For j = 1 To n
If s(j) = 0 And Cost(k, j) < MAX Then
If dist(k) + Cost(k, j) < dist(j) Then
dist(j) = dist(k) + Cost(k, j)
pre(j) = k
If j = t Then
GoTo 200
End If
End If
End If
Next j
100:
Next i
200:
e = pre(t)
If e = 0 Then
GoTo 300
End If
Text1.Text = Text1.Text & "-->" & e
t = e
GoTo 200
300:
Text1.Text = Text1.Text & "==" & dist(tt)
Dim Cost(), DotPos() As Double
Dim DotMaxNo, LineMaxNo As Integer
Const MAX = 1E+38
Dim B_query As Boolean
Dim a(), path() As Double
'DotMaxNo 是最大接点数
'其中cost()中保存有要求解的图的邻接矩阵。
floyd:
ReDim a(DotMaxNo, DotMaxNo), path(DotMaxNo, DotMaxNo)Dim n As Integer
Dim i, j, k, f, t As Integer
n = DotMaxNoFor i = 1 To n
For j = 1 To n
a(i, j) = Cost(i, j)
path(i, j) = 0
Next j
Next iFor k = 1 To n
For i = 1 To n
For j = 1 To n
If a(i, k) + a(k, j) < a(i, j) Then
a(i, j) = a(i, k) + a(k, j)
path(i, j) = k
End If
Next
Next
NextGoTo 300End IfText1.Text = ""
Dim c, d, e As Integer v = Val(Text2.Text)'开始接点
t = Val(Text3.Text)'终止点Text1.Text = Text1.Text & a(f, t) & "=="200:
e = path(f, t)If e = 0 Then
GoTo 300
End If
Text1.Text = Text1.Text & "-->" & e
t = e
GoTo 200
f = e
GoTo 200
300:
’*******************************
dijkstra:
Dim dist(), pre(), s() As Double ReDim dist(DotMaxNo), pre(DotMaxNo), s(DotMaxNo)
Dim i, j, k, min, n, v, t, tt, e As Integer v = Val(Text2.Text)'开始接点
t = Val(Text3.Text)'终止点
tt = t
n = DotMaxNo For i = 1 To n
dist(i) = Cost(v, i)
s(i) = 0
If dist(i) < MAX Then
pre(i) = v
Else
pre(i) = 0
End If
Next s(v) = 1
pre(v) = 0
For i = 1 To n
min = MAX
k = 0
For j = 1 To n
If s(j) = 0 Then
If dist(j) <> 0 And dist(j) < min Then
min = dist(j)
k = j
End If
End If
Next j
If k = 0 Then
GoTo 100
End If
s(k) = 1
For j = 1 To n
If s(j) = 0 And Cost(k, j) < MAX Then
If dist(k) + Cost(k, j) < dist(j) Then
dist(j) = dist(k) + Cost(k, j)
pre(j) = k
If j = t Then
GoTo 200
End If
End If
End If
Next j
100:
Next i
200:
e = pre(t)
If e = 0 Then
GoTo 300
End If
Text1.Text = Text1.Text & "-->" & e
t = e
GoTo 200
300:
Text1.Text = Text1.Text & "==" & dist(tt)
注意看一下矩阵的构建是否有问题。
哎,结帖!