ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingเก่ง vb ช่วยหน่อยคับ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: เก่ง vb ช่วยหน่อยคับ  (อ่าน 669 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
korn89314845
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 5



ดูรายละเอียด
« เมื่อ: 27 มกราคม 2011, 17:07:30 »

คือผมมีโค๊ดหาเส้นทางที่สั้นที่สุดอยู่อะคับ แต่เอามาทำให้เป็นหน้าตาโปรแกรม ทำไม่เป็นอะคับ ช่วยหน่อยคับ มี 2ส่วนนะ

Option Explicit
 
    Const nodeCount As Integer = 9          'number of nodes - 1
    Type DijkEdge
        weight As Integer                   'distance from vertices that it is connected to
        destination As Integer              'name of vertice that it is connected to
    End Type
 
    Type Vertex
        connections(nodeCount) As DijkEdge  'hold information above for each connection
        numConnect As Integer               'number of connections - 1
        distance As Integer                 'distance from all other vertices
        isDead As Boolean                   'distance calculated
        name As Integer                     'name of vertice
    End Type
 
    Public Sub dijkstra_shortest_Path()
        Const infinity As Integer = 15000   'number that is larger than max distance
        Dim i As Integer                    'loop counter
        Dim j As Integer                    'loop counter
        Dim sourceP As Integer              'point to determine distance to from all nodes
        Dim inputData As String             'temp variable to ensure good data enterred
        Dim graph(nodeCount) As Vertex      'all inforamtion for each point (see Vertex declaration above)
        Dim nextP As Integer                'closest point that is not dead
        Dim min As Integer                  'distance of closest point not dead
        Dim outString As String             'string to display the output
        Dim goodSource As Boolean
 
        'user enters source point data and ensured that it is correct
        Do
            goodSource = True
            inputData = (InputBox("What is the source point: ", "Source Point between: 0 & " & nodeCount))
            If IsNumeric(inputData) Then
                sourceP = CInt(inputData)
                If sourceP > nodeCount Or sourceP < 0 Then
                    MsgBox "Source point must be between 0 & " & nodeCount & "."
                    goodSource = False
                End If
            Else
                MsgBox "Source point must be numeric and be between 0 & " & nodeCount & "."
                goodSource = False
            End If
        Loop While Not goodSource
        'get data so we can analyze the distances
        Call populateGraph(graph)
 
        'set default values to not dead and distances to infinity (unless distance is to itself)
        For i = 0 To nodeCount
            If graph(i).name = sourceP Then
                graph(i).distance = 0
                graph(i).isDead = False
            Else:
                graph(i).distance = infinity
                graph(i).isDead = False
            End If
        Next i
 
        For i = 0 To nodeCount
            min = infinity + 1
            'determine closest point that is not dead
            For j = 0 To nodeCount
                If Not graph(j).isDead And graph(j).distance < min Then
                    nextP = j
                    min = graph(j).distance
                End If
            Next j
            'calculate distances from the closest point & to all of its connections
            For j = 0 To graph(nextP).numConnect
                If graph(graph(nextP).connections(j).destination).distance > graph(nextP).distance + graph(nextP).connections(j).weight Then
                    graph(graph(nextP).connections(j).destination).distance = graph(nextP).distance + graph(nextP).connections(j).weight
                End If
            Next j
            'kill the value we just looked at so we can get the next point
            graph(nextP).isDead = True
        Next i
 
        'display the distance from the source point to all other points
        outString = ""
        For i = 0 To nodeCount
            outString = outString & "The distance between nodes " & sourceP & " and " & i & " is " & graph(i).distance & vbCrLf
        Next i
        MsgBox outString
    End Sub



แล้วก็อีกอันนะคับ

Private Sub populateGraph(vertexMatrix() As Vertex)
        'get data into graph matrix to determine distance from all points
        Dim i As Integer
        Dim j As Integer
 
        '0 connections
        vertexMatrix(0).name = 0
        vertexMatrix(0).numConnect = 3
        vertexMatrix(0).connections(0).destination = 1
        vertexMatrix(0).connections(1).destination = 2
        vertexMatrix(0).connections(2).destination = 6
        vertexMatrix(0).connections(3).destination = 7
        vertexMatrix(0).connections(0).weight = 10
        vertexMatrix(0).connections(1).weight = 15
        vertexMatrix(0).connections(2).weight = 30
        vertexMatrix(0).connections(3).weight = 50
 
        '1 connections
        vertexMatrix(1).name = 1
        vertexMatrix(1).numConnect = 3
        vertexMatrix(1).connections(0).destination = 0
        vertexMatrix(1).connections(1).destination = 3
        vertexMatrix(1).connections(2).destination = 4
        vertexMatrix(1).connections(3).destination = 9
        vertexMatrix(1).connections(0).weight = 10
        vertexMatrix(1).connections(1).weight = 16
        vertexMatrix(1).connections(2).weight = 5
        vertexMatrix(1).connections(3).weight = 40
 
        '2 connections
        vertexMatrix(2).name = 2
        vertexMatrix(2).numConnect = 3
        vertexMatrix(2).connections(0).destination = 0
        vertexMatrix(2).connections(1).destination = 7
        vertexMatrix(2).connections(2).destination = 8
        vertexMatrix(2).connections(3).destination = 9
        vertexMatrix(2).connections(0).weight = 15
        vertexMatrix(2).connections(1).weight = 33
        vertexMatrix(2).connections(2).weight = 18
        vertexMatrix(2).connections(3).weight = 60
 
        '3 connections
        vertexMatrix(3).name = 3
        vertexMatrix(3).numConnect = 1
        vertexMatrix(3).connections(0).destination = 1
        vertexMatrix(3).connections(1).destination = 4
        vertexMatrix(3).connections(0).weight = 16
        vertexMatrix(3).connections(1).weight = 22
 
        '4 connections
        vertexMatrix(4).name = 4
        vertexMatrix(4).numConnect = 2
        vertexMatrix(4).connections(0).destination = 1
        vertexMatrix(4).connections(1).destination = 3
        vertexMatrix(4).connections(2).destination = 5
        vertexMatrix(4).connections(0).weight = 5
        vertexMatrix(4).connections(1).weight = 22
        vertexMatrix(4).connections(2).weight = 30
 
        '5 connections
        vertexMatrix(5).name = 5
        vertexMatrix(5).numConnect = 0
        vertexMatrix(5).connections(0).destination = 4
        vertexMatrix(5).connections(0).weight = 30
 
        '6 connections
        vertexMatrix(6).name = 6
        vertexMatrix(6).numConnect = 1
        vertexMatrix(6).connections(0).destination = 0
        vertexMatrix(6).connections(1).destination = 7
        vertexMatrix(6).connections(0).weight = 30
        vertexMatrix(6).connections(1).weight = 40
 
        '7 connections
        vertexMatrix(7).name = 7
        vertexMatrix(7).numConnect = 3
        vertexMatrix(7).connections(0).destination = 0
        vertexMatrix(7).connections(1).destination = 2
        vertexMatrix(7).connections(2).destination = 8
        vertexMatrix(7).connections(3).destination = 6
        vertexMatrix(7).connections(0).weight = 50
        vertexMatrix(7).connections(1).weight = 33
        vertexMatrix(7).connections(2).weight = 3
        vertexMatrix(7).connections(3).weight = 40
 
        '8 connections
       vertexMatrix(Cool.name = 8
       vertexMatrix(Cool.numConnect = 1
       vertexMatrix(Cool.connections(0).destination = 2
       vertexMatrix(Cool.connections(1).destination = 7
       vertexMatrix(Cool.connections(0).weight = 18
       vertexMatrix(Cool.connections(1).weight = 3
 
        '9 connections
       vertexMatrix(9).name = 9
       vertexMatrix(9).numConnect = 1
       vertexMatrix(9).connections(0).destination = 1
       vertexMatrix(9).connections(1).destination = 2
       vertexMatrix(9).connections(0).weight = 40
       vertexMatrix(9).connections(1).weight = 60
    End Sub

« แก้ไขครั้งสุดท้าย: 27 มกราคม 2011, 17:22:00 โดย korn89314845 » บันทึกการเข้า
joesung
NEWS group
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 217
ออฟไลน์ ออฟไลน์

กระทู้: 2,711



ดูรายละเอียด
« ตอบ #1 เมื่อ: 27 มกราคม 2011, 17:15:34 »

มีเร่งให้ตอบด้วย
บันทึกการเข้า

ลายเซ็น:ลายเซ็นจะแสดงที่ด้านล่าง ของแต่ละข้อความ รวมถึงข้อความส่วนตัว คุณสามารถใช้ BBC โค๊ดและสัญลักษณ์แสดงอารมณ์ได้
dragonmath
คนรักเสียว
*

พลังน้ำใจ: 97
ออฟไลน์ ออฟไลน์

กระทู้: 168



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 27 มกราคม 2011, 18:32:49 »

ผมจำอัลกอการหา minimum spaning tree ไม่ได้แล้ว โจทย์ค่อนข้างยากนะครับ
อ่านอัลกอได้ตามข้างล่างครับ
โค๊ด:
http://www.codeproject.com/KB/recipes/Shortest_Path_Problem.aspx
« แก้ไขครั้งสุดท้าย: 27 มกราคม 2011, 18:41:48 โดย dragonmath » บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์