1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/demo/min_route.cc Tue Apr 12 17:33:41 2005 +0000
1.3 @@ -0,0 +1,110 @@
1.4 +#include <lemon/smart_graph.h>
1.5 +#include <lemon/dijkstra.h>
1.6 +#include <lemon/maps.h>
1.7 +#include <lemon/xy.h>
1.8 +#include <lemon/graph_reader.h>
1.9 +
1.10 +#include <lemon/time_measure.h>
1.11 +
1.12 +#include <cmath>
1.13 +
1.14 +using namespace lemon;
1.15 +
1.16 +template <typename CoordMap>
1.17 +class PotentialMap {
1.18 +public:
1.19 + typedef double Value;
1.20 + typedef typename CoordMap::Key Key;
1.21 +
1.22 + PotentialMap(const CoordMap& _coord, const xy<double>& _target)
1.23 + : coord(_coord), target(_target) {}
1.24 +
1.25 + double operator[](const Key& node) const {
1.26 + return std::sqrt((coord[node].x - target.x) * (coord[node].x - target.x) +
1.27 + (coord[node].y - target.y) * (coord[node].y - target.y));
1.28 + }
1.29 +private:
1.30 + xy<double> target;
1.31 + const CoordMap& coord;
1.32 +};
1.33 +
1.34 +template <typename Graph, typename LengthMap, typename PotentialMap>
1.35 +class ReducedLengthMap {
1.36 +public:
1.37 + typedef double Value;
1.38 + typedef typename LengthMap::Key Key;
1.39 +
1.40 + ReducedLengthMap(const Graph& _graph, const LengthMap& _length,
1.41 + const PotentialMap& _pot)
1.42 + : graph(_graph), length(_length), pot(_pot) {}
1.43 +
1.44 + Value operator[](const Key& edge) const {
1.45 + return length[edge] - (pot[graph.source(edge)] - pot[graph.target(edge)]);
1.46 + }
1.47 +
1.48 +private:
1.49 + const Graph& graph;
1.50 + const LengthMap& length;
1.51 + const PotentialMap& pot;
1.52 +};
1.53 +
1.54 +int main() {
1.55 + typedef SmartGraph Graph;
1.56 +
1.57 + typedef Graph::Edge Edge;
1.58 + typedef Graph::Node Node;
1.59 + typedef Graph::EdgeIt EdgeIt;
1.60 + typedef Graph::NodeIt NodeIt;
1.61 + typedef Graph::EdgeMap<double> LengthMap;
1.62 + typedef Graph::NodeMap<xy<double> > CoordMap;
1.63 +
1.64 + SmartGraph graph;
1.65 +
1.66 + GraphReader<Graph> reader(std::cin, graph);
1.67 +
1.68 + CoordMap coord(graph);
1.69 + XMap<CoordMap> xcoord = xMap(coord);
1.70 + reader.addNodeMap("coordinates_x", xcoord);
1.71 + YMap<CoordMap> ycoord = yMap(coord);
1.72 + reader.addNodeMap("coordinates_y", ycoord);
1.73 +
1.74 + LengthMap length(graph);
1.75 + reader.addEdgeMap("length", length);
1.76 +
1.77 + Node source, target;
1.78 + reader.addNode("source", source);
1.79 + reader.addNode("target", target);
1.80 +
1.81 + reader.run();
1.82 +
1.83 + {
1.84 + Timer timer;
1.85 + Dijkstra<Graph, LengthMap> dijkstra(graph, length);
1.86 + dijkstra.init();
1.87 + dijkstra.addSource(source);
1.88 + dijkstra.start(target);
1.89 +
1.90 + std::cout << dijkstra.dist(target) << std::endl;
1.91 + std::cout << timer << std::endl;
1.92 + }
1.93 +
1.94 + {
1.95 + Timer timer;
1.96 + typedef PotentialMap<CoordMap> Potential;
1.97 + Potential potential(coord, coord[target]);
1.98 +
1.99 + typedef ReducedLengthMap<Graph, LengthMap, Potential> ReducedLength;
1.100 + ReducedLength reduced(graph, length, potential);
1.101 +
1.102 + Dijkstra<Graph, ReducedLength> dijkstra(graph, reduced);
1.103 +
1.104 + dijkstra.init();
1.105 + dijkstra.addSource(source);
1.106 + dijkstra.start(target);
1.107 +
1.108 + std::cout << dijkstra.dist(target) + potential[source] << std::endl;
1.109 + std::cout << timer << std::endl;
1.110 + }
1.111 +
1.112 + return 0;
1.113 +}