(2)
请在划线处填入合适的代码。
Const n = 5 '村庄数
Const m = 8 '管道线路数
Dim pointAB(1 To 2 * m) As Integer '存储连接的两个村庄节点
Dim length(1 To m) As Integer '存储两个村庄之间的距离
Dim res(1 To 100) As Integer '存储管道规划结果
Private Sub Form_Load()
'村庄节点数据存储在pointAB数组,两个村庄的距离数据存储在length数组。
'如图c所示,(V1,V3)边距离为1,则pointAB(1)=1,pointAB(2)=3,length(1)=1;
(V1,V2)边距离为6,则pointAB(3)=1,pointAB(4)=2,length(2)=6;其他依次类推,代码略。
End Sub
Function check(x As Integer) As Integer '判断x节点是否已在规划中
check = 0
For i = 1 To 100
If x = res(i) Then check = 1: Exit Function
Next i
End Function
Private Sub Command1_Click()
Dim i As Integer, j As Integer, tmp As Integer
Dim total As Integer, sum As Integer
For i = 1 To m - 1
For j = m To i + 1 Step -1
If ________ Then
tmp= pointAB(2 * j - 1) :pointAB(2 * j - 1) = pointAB(2
* j - 3) :pointAB(2 * j - 3) = tmp
tmp = pointAB(2 * j) : pointAB(2 * j) = pointAB(2 * j -
2) :pointAB(2 * j - 2) = tmp
tmp = length(j): length(j) = length(j - 1): length(j -
1) = tmp
End If
Next j
Next i
'开始规划管道
total = 1
res(1) = pointAB(1): res(2) = pointAB(2)
sum = length(1)
Do While total < ________
For i = 2 To m
If ________ Then
total = total + 1
res(2 * total - 1) = pointAB(2 * i - 1)
res(2 * total) = pointAB(2 * i)
sum = sum + length(i)
Exit For
End If
Next i
Loop
'输出连通结果res 以及管道总长度sum,代码略。
End Sub