gpt4 book ai didi

boost - Boost Graph 中的最长路径

转载 作者:行者123 更新时间:2023-12-01 05:45:11 24 4
gpt4 key购买 nike

对不起,如果这对你们中的一些人来说是一个非常基本的问题,但我是 C++ 的新手(更不用说 Boost Graph Library)并且无法弄清楚这个问题。到目前为止,我已经能够使用下面的代码来制定/收集代码来创建图表。

现在我正在尝试找出在该图中找到最长路径的代码。

有人可以帮忙看看代码是什么吗?在尝试找到路径时,我无法弄清楚是否/如何遍历每个节点和/或边?

我必须尝试返回最长路径中的所有节点和边。

任何帮助将不胜感激。

附言有谁知道 C++ 是否有像 Javadoc 这样的组织文档?

    #include <boost/graph/dag_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <windows.h>
#include <iostream>



int main()
{
using namespace boost;
typedef adjacency_list<vecS, vecS, directedS, property<vertex_distance_t, double>, property<edge_weight_t, double> > graph_t;
graph_t g(6);
enum verts { stationA, stationB, stationC, stationD, stationE, stationF };
char name[] = "rstuvx";


add_edge(stationA, stationB, 5000.23, g);
add_edge(stationA, stationC, 3001, g);
add_edge(stationA, stationD, 2098.67, g);
add_edge(stationA, stationE, 3298.84, g);
add_edge(stationB, stationF, 2145, g);
add_edge(stationC, stationF, 4290, g);
add_edge(stationD, stationF, 2672.78, g);
add_edge(stationE, stationF, 11143.876, g);
add_edge(stationA, stationF, 1, g);




//Display all the vertices
typedef property_map<graph_t, vertex_index_t>::type IndexMap;
IndexMap index = get(vertex_index, g);
std::cout << "vertices(g) = ";

typedef graph_traits<graph_t>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
for (vp = vertices(g); vp.first != vp.second; ++vp.first)
std::cout << index[*vp.first] << " ";
std::cout << std::endl;
// ...

// Display all the edges
// ...
std::cout << "edges(g) = " << std::endl;
graph_traits<graph_t>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
std::cout << "(" << index[source(*ei, g)] << "," << index[target(*ei, g)] << ") \n";
std::cout << std::endl;
// ...

最佳答案

我认为你应该检查一下你的 boost 分布中的例子。
在线:http://www.boost.org/doc/libs/1_38_0/libs/graph/example/dijkstra-example.cpp

为了找到最长的路径,您需要简单地反转权重 (W),使用常数 - W 或 1/W。如果常数为 0,则表示它是一个否定 (-W)。

关于boost - Boost Graph 中的最长路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2815908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com