#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;
}
boost 最短路径 johnson
最新推荐文章于 2019-12-30 13:43:33 发布