我之前的问题得到了解答,但我想知道如何改变这一点以显示最低路线经过的交叉路口?感谢
注意:driverMap是一个2D 14x14整数向量,用于保存到达每个顶点所需的距离
void startFirst(vector< vector<int> > driverMap, vector<Car> allCars, int congestFactor)
{
clock_t start = clock();
int Intersections[driverMap.size()];
int Distances[driverMap.size()];
for(int i = 0; i < driverMap.size(); i++)
{
Intersections[i] = i;
}
for(int i = 0; i < driverMap.size(); i++)
{
cout << "Intersection '" << i << "': ";
for(int k = 0; k < driverMap.size(); k++)
{
cout << driverMap[i][k] << "|";
}
cout << endl;
}
for(int i = 0; i < 1; i++)
{
int startInt = allCars[i].getStart();
Intersections[startInt] = -1;
Distances[startInt] = 0;
for (int i = 0; i < driverMap.size(); i++)
{
if(i != startInt)
{
Distances[i] = driverMap[startInt][i];
}
}
cout << "FOR INTERSECTION: '" << startInt << "'" << endl;
cout << endl;
for (int l = 0; l < driverMap.size(); l++)
{
if(l != startInt)
{
Dijkstra(driverMap, Intersections, Distances);
}
}
for (int k = 0; k < driverMap.size(); k++)
{
cout << Distances[k] << "|";
}
}
cout << "Total time simulated: " << (clock() - start ) / (double) CLOCKS_PER_SEC << endl;
}
void Dijkstra(vector< vector<int> > driverMap, int Intersections[], int Distances[])
{
int minValue = 9999;
int minNode = 0;
for (int i = 0; i < driverMap.size(); i++)
{
if (Intersections[i] == -1)
{
continue;
}
if (Distances[i] > 0 && Distances[i] < minValue)
{
minValue = Distances[i];
minNode = i;
}
}
Intersections[minNode] = -1;
for (int i = 0; i < driverMap.size(); i++)
{
if (driverMap[minNode][i] < 0)
{
continue;
}
if (Distances[i] < 0)
{
Distances[i] = minValue + driverMap[minNode][i];
continue;
}
if ((Distances[minNode] + driverMap[minNode][i]) < Distances[i])
{
Distances[i] = minValue + driverMap[minNode][i];
}
}
}
答案 0 :(得分:3)
下图的手动绘制支持@ hk6279的答案。 &#34; 0&#34;的最佳路径来自&#34; 13&#34;时无法实现。 它应该来自&#34; 1&#34;或&#34; 4&#34;到&#34; 0&#34;并且当@ hk6279注意到并且程序正确计算时,最佳路径是&#34; 5&#34; - &#34; 8&#34; - &#34; 4&#34; - &#34; 0&#34;距离5200。
答案 1 :(得分:2)
您的问题是:假设不正确。 您的假设不正确,因为没有从5到4的直接访问。
您声称可以从5获得0是路径5-4-0
,距离4800
(2600 + 2200)。
但是,事实是您只能通过距离为5-8-4-0
(1500 + 1500 + 2200)的代码获取路径5200
。
请注意,交叉口5中的距离2600
适用于交叉口3,而非交叉口4.您将从索引0开始,而不是1。
答案 2 :(得分:-3)