最短路径实现问题 我正在执行C++语言。基本上,用户输入一个SourceVertex,函数FindShortestPath(int SourceVertex)查找并打印从SourceVertex到所有剩余顶点的最短路径 void Graph::FindShortestPath(int SourceVertex) { cout<<"The shortest paths from "<<SourceVertex<<" are"<<endl; //initialize the ShortestPathArray for(int a=0;a<NumberOfVertices;a++) ShortestPathArray[a]=numeric_limits<int>::max(); ShortestPathArray[SourceVertex]=0; for(int a=0;a<NumberOfVertices;a++) { if(WeightMatrix[SourceVertex][a]!=0) ShortestPathArray[a]=WeightMatrix[SourceVertex][a]; } cout<<"Direct Edges Length"<<endl; for(int a=0;a<NumberOfVertices;a++) { cout<<SourceVertex<<"->"<<a<<"="<<ShortestPathArray[a]<<endl; } cout<<"Shortest Path after updating"<<endl; for(int a=0;a<NumberOfVertices;a++) for(int b=0;b<NumberOfVertices;b++) if(WeightMatrix[a][b]!=0)//edge exists { if(ShortestPathArray[b]>(ShortestPathArray[a]+WeightMatrix[a][b])) { ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];}} for(int a=0;a<NumberOfVertices;a++) cout<<SourceVertex<<"->"<<a<<"="<<ShortestPathArray[a]<<endl;}
打印的第一组是正确的。更新部分出错。我似乎不明白 编辑-1最短路径实现问题 我正在执行C++语言。基本上,用户输入一个SourceVertex,函数FindShortestPath(int SourceVertex)查找并打印从SourceVertex到所有剩余顶点的最短路径 void Graph::FindShortestPath(int SourceVertex) { cout<<"The shortest paths from "<<SourceVertex<<" are"<<endl; //initialize the ShortestPathArray for(int a=0;a<NumberOfVertices;a++) ShortestPathArray[a]=numeric_limits<int>::max(); ShortestPathArray[SourceVertex]=0; for(int a=0;a<NumberOfVertices;a++) { if(WeightMatrix[SourceVertex][a]!=0) ShortestPathArray[a]=WeightMatrix[SourceVertex][a]; } cout<<"Direct Edges Length"<<endl; for(int a=0;a<NumberOfVertices;a++) { cout<<SourceVertex<<"->"<<a<<"="<<ShortestPathArray[a]<<endl; } cout<<"Shortest Path after updating"<<endl; for(int a=0;a<NumberOfVertices;a++) for(int b=0;b<NumberOfVertices;b++) if(WeightMatrix[a][b]!=0)//edge exists { if(ShortestPathArray[b]>(ShortestPathArray[a]+WeightMatrix[a][b])) { ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];}} for(int a=0;a<NumberOfVertices;a++) cout<<SourceVertex<<"->"<<a<<"="<<ShortestPathArray[a]<<endl;},c++,graph,C++,Graph,打印的第一组是正确的。更新部分出错。我似乎不明白 编辑-1 int main(){ Graph g(5); g.AddEdge(0,4,2); g.AddEdge(0,2,3); g.AddEdge(0,1,5); g.AddEdge(1,3,6); g.AddEdge(1,2,2); g.AddEdge(4,3,4); g.AddEdge(4,1,6); g.AddEdge(4,2,10); g.AddEdge(
int main(){
Graph g(5);
g.AddEdge(0,4,2);
g.AddEdge(0,2,3);
g.AddEdge(0,1,5);
g.AddEdge(1,3,6);
g.AddEdge(1,2,2);
g.AddEdge(4,3,4);
g.AddEdge(4,1,6);
g.AddEdge(4,2,10);
g.AddEdge(2,1,1);
g.AddEdge(2,3,2);
g.FindShortestPath(4);
return 0;
}
下面是我的输入代码
if(WeightMatrix[a][b]!=0)//edge exists
{
if(ShortestPathArray[b]>(ShortestPathArray[a]+WeightMatrix[a][b]))
{
ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];
}
}
例如,这里a=0的ShortestPathArray[a]=2147483647;i、 e最大范围,在该值中,您将添加更多值,因此超出范围。
尝试使用比最大限制更小的值。这为我解决了问题 改变
if(WeightMatrix[a][b]!=0)//edge exists
{ if(ShortestPathArray[b]>(ShortestPathArray[a]+WeightMatrix[a][b]))
{
ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];}}
到
添加abs解决了问题。显然,
ShortestPathArray
的某些值是负值。如果最初某个值是numeric\u limits::max()
并且我向其添加了一些内容,则最终结果会变成负值。因此,添加abs()函数很有帮助。请显示发生意外行为的数据。这很有帮助。当我向最大值添加更多值时,它被包装为负值,因此添加了一个abs帮助。是的,如果向最大值添加更多值,它将给出一些-ve值。很高兴知道问题已经解决了。:)<代码>最短路径数组[b]=最短路径数组[a]+权重矩阵[a][b]我认为在这方面你也应该使用absabs
。如果它工作正常,那就没问题了。
if(WeightMatrix[a][b]!=0)//edge exists
{ if(ShortestPathArray[b]>(ShortestPathArray[a]+WeightMatrix[a][b]))
{
ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];}}
if(WeightMatrix[a][b]!=0)//edge exists
{ if(ShortestPathArray[b]>abs(ShortestPathArray[a]+WeightMatrix[a][b]))
{
ShortestPathArray[b]= ShortestPathArray[a]+WeightMatrix[a][b];}}