Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用Dijkstra求boost图中的最短路径_C++_Boost_Graph_Shortest Path - Fatal编程技术网

C++ 用Dijkstra求boost图中的最短路径

C++ 用Dijkstra求boost图中的最短路径,c++,boost,graph,shortest-path,C++,Boost,Graph,Shortest Path,我是boost graph库的新手,我正在尝试使用boost graph库查找图中从一个顶点到另一个顶点的最短路径。我找到了一些关于如何使用前置映射的说明,但它对我不起作用。当我尝试调用p[some_vertex]时,我得到一个错误,表示未定义[]运算符。有人能告诉我为什么和我做错了什么吗 typedef property<edge_weight_t, double, property<edge_index_t, tElementIDVector>> tEdgePrope

我是boost graph库的新手,我正在尝试使用boost graph库查找图中从一个顶点到另一个顶点的最短路径。我找到了一些关于如何使用前置映射的说明,但它对我不起作用。当我尝试调用p[some_vertex]时,我得到一个错误,表示未定义[]运算符。有人能告诉我为什么和我做错了什么吗

typedef property<edge_weight_t, double, property<edge_index_t, tElementIDVector>> tEdgeProperty;
typedef property<vertex_index_t, tElementID> VertexIDPorperty;
typedef adjacency_list<vecS, setS, directedS, VertexIDPorperty, tEdgeProperty> tGraph;

typedef tGraph::vertex_descriptor tVertex;
typedef tGraph::edge_descriptor tEdge;

vector<tVertex> p(num_vertices(graph));
vector<double> d(num_vertices(graph));
// call dijkstra algorithm from boost to find shortest path
dijkstra_shortest_paths(graph, s,
    predecessor_map(&p[0]).
    distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));

//find path from start to end vertex
list<tVertex> pathVertices;
tVertex current = goal;
while (current != start)
{
    pathVertices.push_front(current);
    current = p[current];     //this is where the error occures
}
typedef属性tEdgeProperty;
typedef属性VertexidPerty;
typedef邻接列表tGraph;
typedef tGraph::顶点描述符tVertex;
typedef tGraph::edge_描述符tEdge;
向量p(顶点数(图));
向量d(顶点数(图));
//从boost调用dijkstra算法寻找最短路径
dijkstra_最短路径(图,s,
前置映射(&p[0])。
距离映射(boost::生成迭代器属性映射(d.begin(),get(boost::顶点索引,图));
//查找从起点到终点顶点的路径
列出路径顶点;
tVertex电流=目标;
while(当前!=启动)
{
路径顶点。向前推(当前);
current=p[current];//这是发生错误的地方
}
来自:

std::vector::operator[]

reference       operator[]( size_type pos );
const_reference operator[]( size_type pos ) const;
接受指定向量元素位置的大小

但是,在您的代码中,
current
属于
tVertex
类型


如果您想将
操作符[]
tVertex
参数一起使用,您应该覆盖它。

我无法让您的想法发挥作用,现在也必须进行一些其他更改,这就是为什么我现在将我的前一个映射更改为:

typedef boost::property_map < tGraph, boost::vertex_index_t >::type IndexMap;
typedef boost::iterator_property_map < tVertex*, IndexMap, tVertex, tVertex& > PredecessorMap;
tVertex s = start;
IndexMap indexMap = get(vertex_index, graph);
vector<tVertex> p(num_vertices(graph));
PredecessorMap predecessorMap(&p[0], indexMap);
vector<double> d(num_vertices(graph));

// call dijkstra algorithm from boost to find shortest path
dijkstra_shortest_paths(graph, s, predecessor_map(predecessorMap).distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));
//find path from start to end vertex
list<tVertex> pathVertices;
tVertex current = goal;
while (current != start)
{
    pathVertices.push_front(current);
    current = predecessorMap[current];
}
typedef boost::property\u map::type IndexMap;
typedef boost::迭代器属性映射PredecessorMap;
tVertex s=启动;
IndexMap IndexMap=get(顶点索引,图形);
向量p(顶点数(图));
先人地图先人地图(&p[0],indexMap);
向量d(顶点数(图));
//从boost调用dijkstra算法寻找最短路径
dijkstra_最短路径(图,s,前置映射(前置映射)。距离映射(boost::make_iterator_property_映射(d.begin(),get(boost::vertex_index,graph)));
//查找从起点到终点顶点的路径
列出路径顶点;
tVertex电流=目标;
while(当前!=启动)
{
路径顶点。向前推(当前);
当前=先人地图[当前];
}
这实际上现在起作用了:)