src/demo/min_route.cc
author deba
Tue, 12 Apr 2005 17:38:16 +0000
changeset 1346 639dadf8c00d
child 1358 a81fbbac3b4c
permissions -rw-r--r--
Source and Target EdgeMap.
deba@1342
     1
#include <lemon/smart_graph.h>
deba@1342
     2
#include <lemon/dijkstra.h>
deba@1342
     3
#include <lemon/maps.h>
deba@1342
     4
#include <lemon/xy.h>
deba@1342
     5
#include <lemon/graph_reader.h>
deba@1342
     6
deba@1342
     7
#include <lemon/time_measure.h>
deba@1342
     8
deba@1342
     9
#include <cmath>
deba@1342
    10
deba@1342
    11
using namespace lemon;
deba@1342
    12
deba@1342
    13
template <typename CoordMap>
deba@1342
    14
class PotentialMap {
deba@1342
    15
public:
deba@1342
    16
  typedef double Value;
deba@1342
    17
  typedef typename CoordMap::Key Key;
deba@1342
    18
deba@1342
    19
  PotentialMap(const CoordMap& _coord, const xy<double>& _target)
deba@1342
    20
    : coord(_coord), target(_target) {}
deba@1342
    21
deba@1342
    22
  double operator[](const Key& node) const {
deba@1342
    23
    return std::sqrt((coord[node].x - target.x) * (coord[node].x - target.x) + 
deba@1342
    24
		     (coord[node].y - target.y) * (coord[node].y - target.y));
deba@1342
    25
  }
deba@1342
    26
private:
deba@1342
    27
  xy<double> target;
deba@1342
    28
  const CoordMap& coord;
deba@1342
    29
};
deba@1342
    30
deba@1342
    31
template <typename Graph, typename LengthMap, typename PotentialMap>
deba@1342
    32
class ReducedLengthMap {
deba@1342
    33
public:
deba@1342
    34
  typedef double Value;
deba@1342
    35
  typedef typename LengthMap::Key Key;
deba@1342
    36
deba@1342
    37
  ReducedLengthMap(const Graph& _graph, const LengthMap& _length, 
deba@1342
    38
		   const PotentialMap& _pot) 
deba@1342
    39
    : graph(_graph), length(_length), pot(_pot) {}
deba@1342
    40
deba@1342
    41
  Value operator[](const Key& edge) const {
deba@1342
    42
    return length[edge] - (pot[graph.source(edge)] - pot[graph.target(edge)]);
deba@1342
    43
  }
deba@1342
    44
deba@1342
    45
private:
deba@1342
    46
  const Graph& graph;
deba@1342
    47
  const LengthMap& length;
deba@1342
    48
  const PotentialMap& pot;
deba@1342
    49
};
deba@1342
    50
deba@1342
    51
int main() {
deba@1342
    52
  typedef SmartGraph Graph;
deba@1342
    53
deba@1342
    54
  typedef Graph::Edge Edge;
deba@1342
    55
  typedef Graph::Node Node;
deba@1342
    56
  typedef Graph::EdgeIt EdgeIt;
deba@1342
    57
  typedef Graph::NodeIt NodeIt;
deba@1342
    58
  typedef Graph::EdgeMap<double> LengthMap;
deba@1342
    59
  typedef Graph::NodeMap<xy<double> > CoordMap;
deba@1342
    60
deba@1342
    61
  SmartGraph graph;
deba@1342
    62
deba@1342
    63
  GraphReader<Graph> reader(std::cin, graph);
deba@1342
    64
  
deba@1342
    65
  CoordMap coord(graph);
deba@1342
    66
  XMap<CoordMap> xcoord = xMap(coord);
deba@1342
    67
  reader.addNodeMap("coordinates_x", xcoord);
deba@1342
    68
  YMap<CoordMap> ycoord = yMap(coord);
deba@1342
    69
  reader.addNodeMap("coordinates_y", ycoord);
deba@1342
    70
deba@1342
    71
  LengthMap length(graph);
deba@1342
    72
  reader.addEdgeMap("length", length);
deba@1342
    73
deba@1342
    74
  Node source, target;
deba@1342
    75
  reader.addNode("source", source);
deba@1342
    76
  reader.addNode("target", target);
deba@1342
    77
deba@1342
    78
  reader.run();
deba@1342
    79
deba@1342
    80
  {
deba@1342
    81
    Timer timer;
deba@1342
    82
    Dijkstra<Graph, LengthMap> dijkstra(graph, length);
deba@1342
    83
    dijkstra.init();
deba@1342
    84
    dijkstra.addSource(source);
deba@1342
    85
    dijkstra.start(target);
deba@1342
    86
deba@1342
    87
    std::cout << dijkstra.dist(target) << std::endl;
deba@1342
    88
    std::cout << timer << std::endl;
deba@1342
    89
  }
deba@1342
    90
deba@1342
    91
  {
deba@1342
    92
    Timer timer;
deba@1342
    93
    typedef PotentialMap<CoordMap> Potential;
deba@1342
    94
    Potential potential(coord, coord[target]);
deba@1342
    95
deba@1342
    96
    typedef ReducedLengthMap<Graph, LengthMap, Potential> ReducedLength;
deba@1342
    97
    ReducedLength reduced(graph, length, potential);
deba@1342
    98
deba@1342
    99
    Dijkstra<Graph, ReducedLength> dijkstra(graph, reduced);
deba@1342
   100
deba@1342
   101
    dijkstra.init();
deba@1342
   102
    dijkstra.addSource(source);
deba@1342
   103
    dijkstra.start(target);
deba@1342
   104
  
deba@1342
   105
    std::cout << dijkstra.dist(target) + potential[source] << std::endl;
deba@1342
   106
    std::cout << timer << std::endl;
deba@1342
   107
  }
deba@1342
   108
deba@1342
   109
  return 0;
deba@1342
   110
}