Public Class Form1
Dim best(100, 100) As Integer
Dim dis(100) As Integer
Dim d, i, b, en As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Do While True
d = Val(InputBox("請輸入節點總數", "最短路徑", 1))
b = Val(InputBox("請輸入起點", "最短路徑", 1))
en = Val(InputBox("請輸入終點", "最短路徑", 1))
If Val(b) = Val(en) Or d < 0 Or b < 0 Or en < 0 Or b > d Or en > d Then
MsgBox("輸入錯誤")
Else
Exit Do
End If
Loop
For R = 1 To 100
For P = 1 To 100
best(R, P) = 10000
Next
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(d) < Val(TextBox1.Text) Or Val(d) < Val(TextBox2.Text) Then
MsgBox("輸入錯誤")
Else
best(Val(TextBox1.Text), Val(TextBox2.Text)) = Val(TextBox3.Text)
best(Val(TextBox2.Text), Val(TextBox1.Text)) = Val(TextBox3.Text)
MsgBox("輸入成功!請再輸入其他數值!")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim q, min As Integer
Dim c(100) As Boolean
For m = 1 To 100 Step 1
dis(i) = best(Val(b), i)
c(i) = False
Next
For m = 1 To 100
min = 10000
For j = 1 To 100
If c(j) = False And dis(j) <> 10000 Then
min = dis(j)
q = j
End If
Next
c(q) = True
For v = 1 To 100
If dis(v) > dis(q) + best(q, v) And best(q, v) <> 10000 Then
dis(v) = dis(q) + best(q, v)
dis(v) = min
End If
Next
Next
End Sub
End Class