关于在N个城市办N件大事的问题(大事又由N个小事组成)其中有城市1\2\3\4\5\6\7.....等N个 城市交通只能是1←→2←→3←→4←→5←→6←→7(既不可以从1直接到34567等 也就是城市在一条直线上,要从1到5只能1→2→3→4→5,反之一样)大事A所包含的小事又必须按顺序在3\2\4\3\6\4\1。。等城市完成(每个大事所含的小事数量不一样)
大事B所包含的小事又必须按顺序在1\3\6\2\4\2\1\4。。等城市完成
大事C所包含的小事又必须按顺序在1\4\7\2\4\2\1\4。。等城市完成
大事D.......................................................假设1到2 2到3 等每个城市传送的费用都是200(一样的)怎么样才可以完成所有的事最省钱如果有什么描述不清楚的地方,大家跟贴问

解决方案 »

  1.   

    你就是按照你的条件理出思路三,还有就是可不可以在同一个城市办多个事,你起点的城市是哪里。如果在1的话,就是先在1办B、C;3办B;4办C;5->6办B;7办C;7-6-5-4-3办A;2办A,B,C;3-4办ABC;3办A;2办BC;1办BC;1-2-3-4-5-6办A;6-5-4办A;4-3-2-1办A结果出来就是
    1->3->4->5->6->7->6->5->4->3->2->3->4->2->1->2->3->4->5->6->5->4->3->2->1只是我个人看法哈
      

  2.   

    頭痛了ing...大事A所包含的小事又必须按顺序在3\2\4\3\6\4\1。。等城市完成
    顺序3\2\4\3\6\4\1。。有規律的嗎?
    我笨所以我沒法看出來
      

  3.   

    楼上有点不zhi xiuchi 哦。
      

  4.   

    下面是完整代码,创建一个窗体,然后将代码完整考入代码区。
    楼主!千万不要食言哦!!!Option ExplicitDim SessionA() As String
    Dim SessionB() As String
    Dim SessionC() As String
    Dim BestPath As String
    Dim BestCost As IntegerPrivate Sub Form_Load()
        SessionA = Split("3\2\4\3\6\4\1", "\")
        SessionB = Split("1\3\6\2\4\2\1\4", "\")
        SessionC = Split("1\4\7\2\4\2\1\4", "\")
        
    '下面是一组比较简短的测试数据
    '    SessionA = Split("1\2\3", "\")
    '    SessionB = Split("1\2\1", "\")
    '    SessionC = Split("1\3\2", "\")
        
        
        '开始获得最佳路径
        Debug.Print Now
        Call GetBestPath(0, 0, 0, 1, "")
        Debug.Print Now
        Debug.Print BestPath
        'MsgBox BestPath
    End Sub
    Private Sub GetBestPath(ByVal StepA As Integer, ByVal StepB As Integer, ByVal StepC As Integer, ByVal CurrentCity As Integer, ByVal strPath As String)
        On Error Resume Next
        Dim i As Integer
        Dim intCost As Integer
        DoEvents
        
        
        If StepA <> -1 Then
            If Val(SessionA(StepA)) = CurrentCity Then StepA = StepA + 1            '小任务完成,指向下一步骤
        End If
        If StepB <> -1 Then
            If Val(SessionB(StepB)) = CurrentCity Then StepB = StepB + 1
        End If
        If StepC <> -1 Then
            If Val(SessionC(StepC)) = CurrentCity Then StepC = StepC + 1
        End If
        If StepA > UBound(SessionA) Then StepA = -1                         '步骤值设为-1表示大任务已经完成
        If StepB > UBound(SessionB) Then StepB = -1
        If StepC > UBound(SessionC) Then StepC = -1
        
        strPath = strPath & CurrentCity
            
            
        '*****************************************************
        '计算当前费用,如果当前费用已经大于最小费用那么后面的步骤就没有必要再算了
        '这个判断十分必要,可以大大减少递归的次数
            intCost = 0
            For i = 2 To Len(strPath)
               intCost = intCost + Abs(Val(Mid(strPath, i, 1)) - Val(Mid(strPath, i - 1, 1)))
            Next
            If intCost > BestCost And BestCost <> 0 Then Exit Sub
        '*****************************************************
        
        '三个大任务都完成以后的处理
        If StepA = -1 And StepB = -1 And StepC = -1 Then
            intCost = 0
            '计算费用
            For i = 2 To Len(strPath)
               intCost = intCost + Abs(Val(Mid(strPath, i, 1)) - Val(Mid(strPath, i - 1, 1)))
            Next
            
            If intCost < BestCost Or BestCost = 0 Then BestPath = strPath & "     " & intCost: BestCost = intCost
            '在即时窗口输出所有计算结果
            Debug.Print strPath & "     " & intCost
            Exit Sub
        End If
        
        '临时字符串,用来记录递归分支
        Dim strTemp As String    strTemp = ""
        strTemp = Replace(strTemp, SessionA(StepA), "")
        strTemp = strTemp & SessionA(StepA)
        strTemp = Replace(strTemp, SessionB(StepB), "")
        strTemp = strTemp & SessionB(StepB)
        strTemp = Replace(strTemp, SessionC(StepC), "")
        strTemp = strTemp & SessionC(StepC)    For i = 1 To Len(strTemp)
            Call GetBestPath(StepA, StepB, StepC, Mid(strTemp, i, 1), strPath)
        Next
    End Sub