查找最快的路径

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查找最快的路径相关的知识,希望对你有一定的参考价值。

我有一张火车清单,其中列出了各个车站的到达时间。如果可能,我需要找到两个火车站之间的最快路径。为了做到这一点,我什至被允许在整个旅程中换一次火车。 但是,如果我换火车,我还必须加总上另一趟火车的等待时间。以下是我编写的伪代码,但是在完成伪代码时遇到了问题。特别是在换火车并保持等待时间的同时。任何人都可以帮助我并更正我的伪代码。

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)
答案
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

以上是关于查找最快的路径的主要内容,如果未能解决你的问题,请参考以下文章

在 JSON (Python) 中查找元素的最快方法 [重复]

查找数组的所有元素是不是不同的最快方法?

在数字数组中查找缺失数字的最快方法

linux 基础操作之查找命令

用于最快查找的最佳 MySQL 表结构

执行字符串查找的最快方法?