Source and Target EdgeMap.
1 #include <lemon/smart_graph.h>
2 #include <lemon/dijkstra.h>
3 #include <lemon/maps.h>
5 #include <lemon/graph_reader.h>
7 #include <lemon/time_measure.h>
11 using namespace lemon;
13 template <typename CoordMap>
17 typedef typename CoordMap::Key Key;
19 PotentialMap(const CoordMap& _coord, const xy<double>& _target)
20 : coord(_coord), target(_target) {}
22 double operator[](const Key& node) const {
23 return std::sqrt((coord[node].x - target.x) * (coord[node].x - target.x) +
24 (coord[node].y - target.y) * (coord[node].y - target.y));
28 const CoordMap& coord;
31 template <typename Graph, typename LengthMap, typename PotentialMap>
32 class ReducedLengthMap {
35 typedef typename LengthMap::Key Key;
37 ReducedLengthMap(const Graph& _graph, const LengthMap& _length,
38 const PotentialMap& _pot)
39 : graph(_graph), length(_length), pot(_pot) {}
41 Value operator[](const Key& edge) const {
42 return length[edge] - (pot[graph.source(edge)] - pot[graph.target(edge)]);
47 const LengthMap& length;
48 const PotentialMap& pot;
52 typedef SmartGraph Graph;
54 typedef Graph::Edge Edge;
55 typedef Graph::Node Node;
56 typedef Graph::EdgeIt EdgeIt;
57 typedef Graph::NodeIt NodeIt;
58 typedef Graph::EdgeMap<double> LengthMap;
59 typedef Graph::NodeMap<xy<double> > CoordMap;
63 GraphReader<Graph> reader(std::cin, graph);
65 CoordMap coord(graph);
66 XMap<CoordMap> xcoord = xMap(coord);
67 reader.addNodeMap("coordinates_x", xcoord);
68 YMap<CoordMap> ycoord = yMap(coord);
69 reader.addNodeMap("coordinates_y", ycoord);
71 LengthMap length(graph);
72 reader.addEdgeMap("length", length);
75 reader.addNode("source", source);
76 reader.addNode("target", target);
82 Dijkstra<Graph, LengthMap> dijkstra(graph, length);
84 dijkstra.addSource(source);
85 dijkstra.start(target);
87 std::cout << dijkstra.dist(target) << std::endl;
88 std::cout << timer << std::endl;
93 typedef PotentialMap<CoordMap> Potential;
94 Potential potential(coord, coord[target]);
96 typedef ReducedLengthMap<Graph, LengthMap, Potential> ReducedLength;
97 ReducedLength reduced(graph, length, potential);
99 Dijkstra<Graph, ReducedLength> dijkstra(graph, reduced);
102 dijkstra.addSource(source);
103 dijkstra.start(target);
105 std::cout << dijkstra.dist(target) + potential[source] << std::endl;
106 std::cout << timer << std::endl;