-#ifdef DEBUG
-#define ASSERT(x) if(!(x)) { \
- std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
- abort(); \
-}
-#else
-#define ASSERT(x)
-#endif
-
-class Vertex;
-
-class Edge {
-public:
- int id, occupied;
- scalar_t length, work_length;
- Vertex *terminal_vertex;
- Edge *next, *pred;
-};
-
-class Vertex {
-public:
- int id;
-
- Edge *root_edge;
- scalar_t distance_from_source;
-
- Vertex *pred_vertex;
- Edge *pred_edge;
-
- Vertex() { root_edge = 0; }
-
- inline void add_edge(Edge *e) {
- e->next = root_edge;
- e->pred = 0;
- if(root_edge) { root_edge->pred = e; }
- root_edge = e;
- }
-
- inline void del_edge(Edge *e) {
- if(e == root_edge) { root_edge = e->next; }
- if(e->pred) { e->pred->next = e->next; }
- if(e->next) { e->next->pred = e->pred; }
- }
-};
-
-class Graph {
- void initialize_work_lengths();
- void update_work_length();
- void find_shortest_path(Vertex **front, Vertex **new_front);
-
- int nb_vertices;
- Edge *edge_heap;
- Vertex *vertices;
- Vertex *source, *sink;
-
-public:
- Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
- int source, int sink);
-
- ~Graph();
-
- void find_best_paths(int *result_edge_occupation);
- void print();
-};
-
-void Graph::print() {
- for(int n = 0; n < nb_vertices; n++) {
- for(Edge *e = vertices[n].root_edge; e; e = e->next) {
- cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
- if(e->occupied) {
- cout << " *";
- }
- cout << endl;
- }
- }
-}
-
-Graph::Graph(int nb_vrt, int nb_edges,
- int *from, int *to, scalar_t *lengths,
- int src, int snk) {
- nb_vertices = nb_vrt;
-
- edge_heap = new Edge[nb_edges];
- vertices = new Vertex[nb_vertices];
-
- source = &vertices[src];
- sink = &vertices[snk];
-
- for(int v = 0; v < nb_vertices; v++) {
- vertices[v].id = v;
- }
-
- for(int e = 0; e < nb_edges; e++) {
- vertices[from[e]].add_edge(&edge_heap[e]);
- edge_heap[e].occupied = 0;
- edge_heap[e].id = e;
- edge_heap[e].length = lengths[e];
- edge_heap[e].terminal_vertex = &vertices[to[e]];
- }
-}
-
-Graph::~Graph() {
- delete[] vertices;
- delete[] edge_heap;
-}
-
-void Graph::initialize_work_lengths() {
- scalar_t length_min = 0;
- for(int n = 0; n < nb_vertices; n++) {
- for(Edge *e = vertices[n].root_edge; e; e = e->next) {
- length_min = min(e->length, length_min);
- }
- }
- for(int n = 0; n < nb_vertices; n++) {
- for(Edge *e = vertices[n].root_edge; e; e = e->next) {
- e->work_length = e->length - length_min;
- }
- }
-}