boost 最短路径 johnson

#include <exception>
#include <boost/config.hpp>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>

int
main()
{
    using namespace boost;
    typedef adjacency_list<vecS, vecS, undirectedS, no_property,
        property< edge_weight_t, int, property< edge_weight2_t, int > > > Graph;

    const int V = 4;
    typedef std::pair < int, int >Edge;
    Edge edge_array[] =
    {
        Edge(0, 1), Edge(0, 2), Edge(1, 2), Edge(2, 3),
    };
    const std::size_t E = sizeof(edge_array) / sizeof(Edge);


    Graph g(edge_array, edge_array + E, V);


    property_map < Graph, edge_weight_t >::type w = get(edge_weight, g);
    int weights[] = {1,4,2,5};
    int *wp = weights;

    graph_traits < Graph >::edge_iterator e, e_end;
    for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
        w[*e] = *wp++;

    //std::vector < int >d(V, (std::numeric_limits < int >::max)());
    int distanceMatrix[V][V];
    johnson_all_pairs_shortest_paths(g, distanceMatrix/*, distance_map(&d[0])*/);

    //std::cout << "     ";
    //for (int k = 0; k < V; ++k)
    //    std::cout << std::setw(5) << k;
    //std::cout << std::endl;
    //for (int i = 0; i < V; ++i) {
    //    std::cout << i << " -> ";
    //    for (int j = 0; j < V; ++j) {
    //        if (distanceMatrix[i][j] > 20 || distanceMatrix[i][j] < -20)
    //            std::cout << std::setw(5) << "inf";
    //        else
    //            std::cout << std::setw(5) << distanceMatrix[i][j];
    //    }
    //    std::cout << std::endl;
    //}

    //std::ofstream fout("figs/johnson-eg.dot");
    //fout << "digraph A {\n"
    //    << "  rankdir=LR\n"
    //    << "size=\"5,3\"\n"
    //    << "ratio=\"fill\"\n"
    //    << "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n";

    //graph_traits < Graph >::edge_iterator ei, ei_end;
    //for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
    //    fout << source(*ei, g) << " -> " << target(*ei, g)
    //    << "[label=" << get(edge_weight, g)[*ei] << "]\n";

    //fout << "}\n";
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值