我之前的问题得到了回答,但我想知道如何修改这个以显示最低路线经过的交叉口谢谢
注意:drivermap是一个2d14x14整数向量,它包含到达每个顶点所需的距离
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];
}
}
}
最佳答案
你的问题是:错误的假设。你的假设是不正确的,因为没有从5到4的直接访问。
你所说的是从5到0是路径5-4-0
和距离4800
(2600+2200)。
然而,事实是,只有使用distance5-8-4-0
(1500+1500+2200)的代码才能获得path5200
。
请注意,5号交叉口的距离2600
是3号交叉口的距离,而不是4号交叉口的距离从索引0开始,而不是从索引1开始。