最短路径算法C ++ Dijkstra的包含Layover

时间:2017-11-30 12:58:03

标签: c++ algorithm shortest-path dijkstra

大家早上好,

我有一个问题,我必须计算从一个火车站到另一个火车站的最短路径(包括中途停留)。我通常不会寻求帮助,但我非常坚持这一点。我开始编写一个标准的dijkstra算法,并认为我可以修改它以进行中途停留,但事实并非如此。该程序设置为[stationCount] [stationCount]大小的邻接矩阵,由边缘结构组成。每个边缘结构都有:

int departure; int arrival; int totalTime; (arrival - departure)

并且所有时间都在军事时间(0900,1230等)。我遇到问题的方法在下面发布,我相当肯定我的问题是计算/添加中途停留。我已经评论过那个标记它的部分。

最后,这是我的输入数据:

1 2 0430 0600
1 4 0550 1000 
2 4 0630 1100 
2 5 1130 1300 
3 1 1245 1500 
3 6 0300 0530 
4 5 0730 0945 
4 7 1145 1600 
4 6 1500 1900 
4 3 1000 1330 
5 7 1430 1730 
7 6 1550 1800

我已经要求程序尝试从(1,6)返回最短路径,但它没有返回有效路径。我计算出的最短路径是1-> 4(0450)+(0500)-layover + 4-> 6(0400)=(1350),然后我可以将其转换为13小时10分钟。它可以采取多种其他路径,但所有这些路径都可以提前开始并在同一时间结束,或者稍后将它们设置为更长的路径。如果有人对我的错误或更好的方法有任何指导,请告诉我。谢谢。

int TRAINS::dijkstra(int source, int destination)
{
    vector <bool> vertexSet;
    int distance[stationCount];
    int minNode, minDistance, source, destination, count = 0, previousNode;

    for(int i = 1; i < stationCount; ++i)
    {
        distance[i] = INFINITY;
        vertexSet.push_back(false);
    }

    distance[source] = 0;
    while((!empty(vertexSet)) && count < stationCount - 1)
    {
        count++;
        minNode = INFINITY;
        minDistance = INFINITY;

        for(int vertex = 1; vertex < stationCount; ++vertex)
        {
            if(vertexSet[vertex] == false && distance[vertex] <= minDistance)
            {
                minDistance = distance[vertex];
                minNode = vertex;
            }
        }

        vertexSet[minNode] = true;

        //so what i tried to do is that if it is the first time running through (i.e. source == minNode then it will set the distance for all verteces to .totalTime (the regular weight) and that seems to work
        for(int vertex = 1; vertex < stationCount; ++vertex)
        {
            if(vertexSet[vertex] == false && matrix[minNode][vertex].arrival > 0)
            {
                if(minNode == source)
                {
                    if(distance[minNode] != INFINITY && matrix[minNode][vertex].totalTime < distance[vertex])
                    {
                        distance[vertex] = matrix[minNode][vertex].totalTime;

                        if(vertex == destination)
                        {
                            return distance[vertex];
                        }
                    }
                }

                //then here if it wasn't the first time (this is where I am having trouble... i was trying to have it take the current departure time - the previous arrival time to calculate the layover and then add it to the current distance (distance[minNode]) plush the actual weight still... but somewhere this is going wrong and i am not getting the right output.
                else
                {
                    if(distance[minNode] != INFINITY && matrix[minNode][vertex].departure > 0 && matrix[minNode][vertex].departure - matrix[previousNode][minNode].arrival + matrix[minNode][vertex].totalTime + distance[minNode] < distance[vertex])
                    {
                        distance[vertex] = matrix[minNode][vertex].departure - matrix[previousNode][minNode].arrival + matrix[minNode][vertex].totalTime + distance[minNode];
                    }
                    if(vertex == destination)
                    {
                        return distance[vertex];
                    }
                }
            }
        }
        //before the next cycle i set the current minNode to previous for use in the next cycle
        previousNode = minNode;
    }
    return -1;

}

-

0 个答案:

没有答案