【问题标题】:how to use Dijkstra c++ code using array based version如何使用基于数组的版本使用 Dijkstra C++ 代码
【发布时间】:2010-09-22 01:13:56
【问题描述】:

我需要使用(不实现)基于数组的 Dijkstras 算法版本。任务是给定一组线段(障碍)和起点/终点,我必须找到并绘制从起点/终点开始的最短路径点。我已经完成了计算部分等。但不知道如何在我的代码中使用dijkstras。我的代码如下

class Point
{
public:
int x;
int y;
Point()
{

}

void CopyPoint(Point p)
{
this->x=p.x;
this->y=p.y;
}
};

class NeighbourInfo
{
public:
Point x;
Point y;
double distance;

NeighbourInfo()
{
distance=0.0;
}
};


class LineSegment
{
 public:
 Point Point1;
 Point Point2;
 NeighbourInfo neighbours[100];

 LineSegment()
 {

 }




 void main()//in this I use my classes and some code to fill out the datastructure
 {

 int NoOfSegments=i;

 for(int j=0;j<NoOfSegments;j++)
 {
 for(int k=0;k<NoOfSegments;k++)
 {
 if( SimpleIntersect(segments[j],segments[k]) )
 {
   segments[j].neighbours[k].distance=INFINITY;
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);
   cout<<"Intersect"<<endl;
   cout<<segments[j].neighbours[k].distance<<endl;
 }
else
{
   segments[j].neighbours[k].distance=
EuclidianDistance(segments[j].Point1.x,segments[j].Point1.y,segments[k].Point2.x,segments[k    ].Point2.y);
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);

}
}
}

}

现在我有了从每个 segmnet 到所有其他段的距离,amd 使用这个数据(在 neighbourinfo 中)我想使用基于数组的 Dijkstras(restriction) 来追踪从起点/终点开始的最短路径。还有更多代码,但为了方便读者缩短了问题

请帮忙!!谢谢,请不要 .net lib/code,因为我只使用核心 C++。提前谢谢

但我需要基于数组的版本(严格来说..)我不打算使用任何其他实现。

【问题讨论】:

  • 并不是说它与您的问题有任何关系,但您最好使用 int main() 并且通常在编码时考虑到 ANSI。
  • 用空格替换制表符如何使其可读! (或任何导致格式错误的原因)。
  • 我需要一个基于数组的版本
  • 什么叫“基于数组的版本”?如果你有,它的 API 是什么?如果你不这样做,使用 boost.Graph 怎么样?
  • @Andreas:我想我们可以。 “问题”标签怎么样?

标签: c++ dijkstra


【解决方案1】:

迪克斯特拉斯

这是 Dijkstra 的工作原理:
它不是一个简单的算法。因此,您必须将此算法映射到您自己的代码。
但祝你好运。

List<Nodes>    found;     // All processed nodes;
List<Nodes>    front;     // All nodes that have been reached (but not processed)
                          // This list is sorted by the cost of getting to this node.
List<Nodes>    remaining; // All nodes that have not been explored.

remaining.remove(startNode);
front.add(startNode);
startNode.setCost(0); // Cost nothing to get to start.

while(!front.empty())
{
    Node       current = front.getFirstNode();
    front.remove(current);
    found.add(current);

    if (current == endNode)
    {    return current.cost(); // we found the end
    }

    List<Edge> edges   = current.getEdges();

    for(loop = edges.begin(); loop != edges.end(); ++loop)
    {
        Node   dst = edge.getDst();
        if (found.find(dst) != found.end())
        {   continue; // If we have already processed this node ignore it.
        }


        // The cost to get here. Is the cost to get to the last node.
        // Plus the cost to traverse the edge.
        int    cost = current.cost() + loop.cost();
        Node   f    = front.find(dst);
        if (f != front.end())
        {
            f.setCost(std::min(f.cost(), cost));
            continue;  // If the node is on the front line just update the cost
                       // Then continue with the next node.
        }

        // Its a new node.
        // remove it from the remaining and add it to the front (setting the cost).
        remaining.remove(dst);
        front.add(dst);
        dst.setCost(cost);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多