【问题标题】:Find fastest path找到最快的路径
【发布时间】:2020-04-24 09:44:49
【问题描述】:

我有一个火车列表,其中包含各个车站的到达时间。

如果可能,我需要找到两个火车站之间最快的路径。

为了做到这一点,我甚至允许在整个旅程中换车一次。
但是,如果我换车,我还必须在上另一辆火车时加上等待时间。
以下是我的伪代码已经写了,但我在完成它时遇到了问题。尤其是在换车和计算等待时间的时候。谁能帮我解决这个问题并纠正我的伪代码。

function findFastestPath(source_station,dest_staion,waiting_time_at_current_station,travel_time_in_previous_train):
    for each train_schedule in TrainList:
        while train_schedule is not over:
            if source_station equals train_schedule[station]
            Then
                if dest_staion in train_schedule
                Then
                    if dest_staion[departure_time] > source_station[departure_time]
                    Then
                        duration = dest_staion[departure_time] - source_station[departure_time] + waiting_time_at_current_station + travel_time_in_previous_train
                        if mintime > duration
                        Then
                            mintime = duration
                else:
                   travel_time_in_previous_train = next_station_to_source_station[departure_time]-source_station[departure_time]
                   waiting_time_at_current_station = next_train_departure_time - next_station_to_source_station[departure_time]
                   call findFastestPath(next_station_to_source_station, dest_staion, waiting_time_at_current_station, travel_time_in_previous_train)            
        return mintime
end function

for each source_station in Travel_Time_Matrix:
    for each dest_staion in Travel_Time_Matrix:
        call findFastestPath(source_station,dest_staion,0,0)

【问题讨论】:

    标签: algorithm pseudocode dijkstra


    【解决方案1】:
    FindFastestPathInSameTrain(src,dest):
        For x in range(Len(TrainList)):                                    //TrainList is a two dimentional array which has each train schedule in each row
            IF src and dest in TrainList[x]:                               
                IF dest(time) > src(time):                                 // Here we are checking if destination station is not before source station in train
                    duration = src->departuretTime - dest->departuretTime  // calculating duration
                    IF min > duration:                                     // min value will be updated if as we find fastest fast in other trains
                        min = duration
            else:
                NewTrainList.add(TrainList[x])                             // if line 2 is not satisfied we will add that train in NewTrainList which is 2d array
        return min                                                 // by default  min value will be 1000, and it will be returned in case of no direct train
    
    FindFastestPathInSecondTrain(src,dest):                                // now we will work on trains which dont has both source and destination station
       For x in range(Len(NewTrainList)):                                  // we will iterate NewTrainList
           IF src in NewTrainList[x]:                                      // if we find our Source station in NewTrainList
               For n in range(NewTrainList[x].index(src)+1,len(NewTrainList[x])): // we will iterate that trains remaining station as src in a new extended function
                   FindFastestPathInSecondTrainExtension(NewTrainList[n],dest,src->departuretTime) 
    
    // this comment is for line 16 : Just as our FindFastestPathInSameTrain(), but here we will send origin departuretTime so as to capture time spent in prev train + waiting time at connecting station.
    
    FindFastestPathInSecondTrainExtension(newsrc,dest,src->departuretTime): // This function will work similar to FindFastestPathInSameTrain()
        For x in range(Len(NewTrainList)):
            IF newsrc and dest in NewTrainList[x]:
                IF dest->departuretTime > newsrc->departuretTime
                    secondDuration = (dest->departuretTime - newsrc->departuretTime) + (newsrc->departuretTime - src->departuretTime)
                    IF min > secondDuration
                       min = secondDuration
        return min
    
    For each source_station in Travel_Time_Matrix:                         // Iterating all the source_station
        For each dest_staion in Travel_Time_Matrix:                        // Iterating all the dest_station
            T1 = FindFastestPathInSameTrain(source_station,dest_station)   
            T2 = FindFastestPathInSecondTrain(source_station,dest_station)
            IF T1 < T2
               write T1
            ELSE:
               write T2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      相关资源
      最近更新 更多