临近学考,学习vb,于是打个spfa试试

Const maxn = 1000

Dim v(maxn), dis(maxn), nxt(maxn * 2), va(maxn * 2), first(maxn * 2), cnt As Integer
Dim n As Integer
Dim q(maxn * 5), head, tail As Integer
Dim inq(maxn) As Boolean
Public Sub addedge(u As Integer, vv As Integer, vaa As Integer)
cnt = cnt + 1
nxt(cnt) = first(u)
first(u) = cnt
va(cnt) = vaa
v(cnt) = vv
End Sub

Private Sub Command1_Click()
For i = 1 To n
dis(i) = 32767
Next i
q(1) = Val(Text2.Text)
dis(q(1)) = 0
head = 1
tail = 1
While tail >= head
Dim now, x As Integer
x = q(head)
now = first(q(head))
While (now <> 0)
If dis(v(now)) > dis(x) + va(now) Then
If inq(v(now)) Then
tail = tail + 1
q(tail) = True
End If
dis(v(now)) = dis(x) + va(now)
End If
now = nxt(now)
Wend
inq(x) = False
head = head + 1
Wend
For i = 1 To n
List1.AddItem (Str(i) + " " + Str(dis(i)))
Next i

End Sub

Private Sub Command2_Click()
Call addedge(Val(Text1.Text), Val(Text3.Text), Val(Text4.Text))
End Sub

Private Sub Text5_Change()
n = Val(Text5.Text)
End Sub