当前位置: 高中信息技术 / 综合题
  • 1. (2021高三上·浙江月考) 试卷运送。五校联考就要到了,本次负责安排运送试卷的路线的是TBY。由于各所学校都参与了一些科目命题工作,所以目前每个学校里都有几个科目的试卷。为了节省运输费用,TBY想要找到一种能连接所有学校且线路总长最短的方案。

    一共有n(3<=n<=100)个学校,各所学校之间的线路长度以一行共n*n个数据输入,

    第i组的n个数据为从第i个学校出发到其他学校的线路长度,保证第i组的第i个数据为0。

    编写VB程序,实现上述功能。运行程序,在文本框Text1中输入学校总数,

    在文本框Text2中按题述方法输入各校之间的距离,点击命令按钮Command1后,在标签Label1中显示最短的线路总长。程序运行界面如图所示。

    请回答下列问题:

    1. (1) 若n=4,各校距离为0242831240352528350303125300,则最短的线路总长为
    2. (2) 实现上述功能的VB程序如下,请在划线处填入合适代码。

      Const INF = 2147483646

      Dim n As Long, a As Long

      Dim dis(1 To 10001) As Long, flag(1 To 10001) As Boolean

      Dim head(1 To 10001) As Long, nxt(1 To 10001) As Long

      Dim ed(1 To 10001) As Long, value(1 To 10001) As Long

      Dim cnt As Long, tby As Long, sss As String

      Dim i As Long, j As Long

      Private Sub Command1_Click()

          cnt = 0

          n = Val(Text1.Text)

          For i = 1 To n

              dis(i) = INF : flag(i) = False : head(i) = 0

          Next i

          tby = 1

          sss = Text2.Text

          For i = 1 To n

              For j = 1 To n

                 

                  If i <> j Then Call addedge(i, j, a)

              Next j

          Next i

          Dim ans As Long

          dis(1) = 0 : ans = 0

          For i = 1 To n

              Dim hg As Long, u As Long

              hg = INF

              u = 0

              For j = 1 To n

                  If  Then

                      hg = dis(j)

                      u = j

                  End If

              Next j

             

              flag(u) = True

              Dim p As Long

              p = head(u)

              Do While p <> 0

                  Dim v As Long

                  v = ed(p)

                  If Not flag(v) And dis(v) > value(p) Then

                      dis(v) = value(p)

                  End If

                  p = nxt(p)

              Loop

          Next i

          Label1.Caption = Str(ans)

      End Sub

      Sub addedge(a As Long, b As Long, x As Long)

          cnt = cnt + 1

          value(cnt) = x

          ed(cnt) = b

          nxt(cnt) = head(a)

          head(a) = cnt

      End Sub

      Function Getin() As Long

          Dim aaa As String

          aaa = Mid(sss, tby, 1)

          If aaa = " " Then tby = tby + 1

          aaa = Mid(sss, tby, 1)

          Dim bbb As Long

          bbb = 0

          Do While aaa <> " " And tby <= Len(sss)

              bbb = bbb * 10 + Val(aaa) : tby = tby + 1

              aaa = Mid(sss, tby, 1)

          Loop

          Getin = bbb

      End Function

微信扫码预览、分享更方便